-5
我有對象數組像查找對象數組最短路徑在JavaScript
const obs = [
{ from: "A", to: "C", dis: 5},
{ from: "A", to: "D", dis: 4},
{ from: "D", to: "C", dis: 8},
{ from: "C", to: "B", dis: 9},
{ from: "B", to: "D", dis: 17},
]
現在我已經從A找到B. 最短路徑dis
所以FR我已創建二維數組
findUniqueEndPoints() {
let _endPoints = []
obs.forEach(e => {
if (!_endPoints.includes(e.from)) {
_endPoints.push(e.from)
}
if (!_endPoints.includes(e.to)) {
_endPoints.push(e.to)
}
})
return _endPoints
}
this.endPoints = this.findUniqueEndPoints()
let _matrix = []
this.endPoints.forEach((e, i) => {
//const valus = obs.map(o => o.from === e ? o.dis : null)
_matrix[i] = this.endPoints.map(() => 0)
})
obs.forEach(e => {
_matrix[this.endPoints.indexOf(e.from)][this.endPoints.indexOf(e.to)] = e.dis
})
console.log(_matrix)
// logs
//[[0, 5, 4, 0][0, 0, 0, 9][0, 8, 0, 0][0, 0, 17, 0]]
很酷,你過得怎麼樣?你有什麼嘗試? –
_現在我必須找到從A到B的最短路徑DIS。您的問題是什麼? –
@bub請檢查我有更新的問題。 –