Infinite Loop: BFS, aby uzyskać wszystkie wartości zagnieżdżonych obiektów (JavaScript)
const schedule = {
date: '0305',
meeting1: {
id: '00001',
start: '10:30'
},
meeting2: {
id: '00002',
start: '12:30'
}
};
function getNestedObjValuesByQueue(obj){
let queue = [obj]
let values = []
while (queue.length > 0){
let current = queue.shift();
for (let val of Object.values(current)){
if(typeof val === "object") queue.push(val);
else values.push(val);
}
}
console.log(values)
}
getNestedObjValuesByQueue(schedule)
Run code snippet
Inexpensive Ibis