-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请检查我有更新的问题。 –