I am trying to get all the values of the nested object by BFS: This is the sample object:
let schedule =
{
date: '0305',
meeting1: {
id: '00001',
start: '10:30'
},
meeting2: {
id: '00002',
start: '12:30'
}
}
Ideal output:
['0305', '00001', '10:30', '00002', '12:30']
My attempt:
function getNestedObjValuesByQueue(obj){
let queue = [obj]
let values = []
while (queue.length > 0){
let current = queue.shift();
console.log(current);
values = values.concat(Object.keys(current));
for (let key of Object.values(current)){
queue.push(key)
}
}
// console.log(values)
}
getNestedObjValuesByQueue(schedule)
Well when I tried to log current there comes infinite loop:
5
0
0
0
0
1
1
0
:
3
0
0
0
0
0
2
1
2
:
3
0
0
3
0
5
0
0
0
0
1
1
0
:
Still have no idea what is going on here... Can anyone help me out? Thanks in advance.