I have the following object structure:
{
a: {
b: "abc",
c: [
{
d: "pqr",
e: true
},
{
f: 1,
e: ["xyz", 1]
}
]
}
}
Now I want to convert these structure in specific format that are shown in expected output.
I'm trying to traverse above structure but the constructed path not correct Here is what I'm doing
const getFinalNode = function(data, path){
if (typeof data === 'object'){
for (let key in data) {
// console.log(key);
if (typeof data[key] === 'object'){
path += key+'/'
getFinalNode(data[key], path)
// console.log(true);
}else{
console.log({
value: data[key],
key: key,
path:path
})
}
}
}
}
getFinalNode(data, "");
My expected output should be
[
{
name: "b",
value: "abc",
path: "a/b"
},
{
name: "d",
value: "pqr",
path: "a/c/0/d"
},
{
name: "e",
value: true,
path: "a/c/0/e"
},
{
name: "f",
value: 1,
path: "a/c/1/f"
},
{
name: "e",
value: 'xyz',
path: "a/c/1/e"
},
{
name: "e",
value: 1,
path: "a/c/1/e"
}
]
So how can I traverse above structure and convert it in expected output or is there any solution that can I use in my existing code.
Now I getting following output

a/b/c/0/d, instead ofa/c/0/d? Neat question by the way. :-)e: ["xyz", 1]be separated into two objects (like the arraycis)?