I'm trying to find a object value inside a nested object, this object has a lot o level's and this specific object value that i want it's not always in the same level inside the object.
What i'm trying to do is:
//ArrayofCoordinates it's the array of the objects that i want to search my value inside of each of this objects.
let coordinatesToAddArray = [];
arrayOfCoordinates.map(coordinateObject => {
let coordinateData = findObjectNested(coordinateObject, 'tagName', 'text'); //'tagName' it's the key that i'm looking for and 'text' it's the value of the 'tagName' key that i'm looking for.
if(coordinateData) {
coordinatesToAddArray.push(coordinateData);
}
});
//This is the recursive function that i created to search inside the object for the specific value and return him, but all i get from the return of this function it's a undefined.
function findObjectNested(entireObj, keyToFind, valueToFind) {
if(entireObj && entireObj[keyToFind] === valueToFind) {
return entireObj;
} else {
if(entireObj['children'].length > 0) {
entireObj['children'].map(objectsOfChildren => {
findObjectNested(objectsOfChildren, keyToFind, valueToFind);
});
}
}
}
I know that the recursive function it's working because when i replace the "return entireObj" to "console.log(entireObj)" i can see the object that i'm looking for, but when i'm trying to return this object all i get it's a undefined.
I believe that this has something to do with the recursive function.
Anyone can help me with any tips about how to solve this problem?
Thank you so much.