I have an object that looks like the following:
const testObject = {
"NameA": {
"Name": {
"_text": "Peter"
}
},
"AgeA": {
"_comment": "line=2",
"age": {
"_text": "21"
}
},
"Birth": {
"_comment": "line=3",
"Birthday": {
"DateTimeSignUTCOffset": {
"Date": {
"_text": "191201"
},
"Time": {
"_text": "1123"
},
},
"Test": {
"Code": {
"_text": "1234"
}
},
}
}
}
I am trying to find any key with the key _text and get the corresponding value and the parent key.
i.e.
const result = {
"Name": "Peter",
"age": "21",
"Date": "191201",
"Time": "1123",
"Code": "1234"
};
I have tried the following by looping through the object but am unable to figure it out.
const result = {};
const find_items = (object) => {
console.log(Object.keys(object));
Object.keys(object).map((item) => {
console.log(object[item]);
if(object[item] !== '_text') {
find_items(object[item])
} else {
console.log(item)
}
});
};
find_items(testObject);
console.log(result);
Can someone could point me in the right direction?