I am a beginner in javaScript, I have this code and i want to get the values of the key ‘a’ in the object MyGraph using the “currentVertex” variable which is in this situation has the value ‘a’. how to fix this instruction: console.log(Object.values(MyGraph.currentVertex));
My code :
let unvisitedVertices={ 'a' :0, 'b':Infinity, 'c':Infinity, 'd':Infinity, 'e':Infinity, 'f':Infinity};
const MyGraph = {
a: { b: 5, c: 2 },
b: { a: 5, c: 7, d: 8 },
c: { a: 2, b: 7, d: 4, e: 8 },
d: { b: 8, c: 4, e: 6, f: 4 },
e: { c: 8, d: 6, f: 3 },
f: { e: 3, d: 4 },
};
const minDistance =Math.min.apply(Math,Object.values(unvisitedVertices));
function calcDistance() {
}
function getKeyByValue(object, value) {
return Object.keys(object).find(key => object[key] === value);
}
const currentVertex = getKeyByValue(unvisitedVertices,minDistance);
console.log(currentVertex);
console.log(minDistance);
console.log(Object.values(MyGraph.currentVertex));
After execution :
console.log(Object.values(MyGraph.currentVertex));
TypeError: Cannot convert undefined or null to object
at Function.values (<anonymous>)
at Object.<anonymous> (c:UsersInspiron15DesktopsummerjavaScripttest.js:25:21)
at Module._compile (node:internal/modules/cjs/loader:1103:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1157:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
at node:internal/main/run_main_module:17:47 ```