I have a deeply nested data structure and I am interested in matching a certain value inside my array (and array of arrays) and then pushing some data inside an accompanying array. For example following is my array of colors and accompanied is a moreColors array which may or may not exist :
var myData = [{
"color": "green",
"moreColors": [
{
"color": "beige"
},
{
"color": "black",
"moreColor": [
{
"color": "grey"
},
{
"color": "white",
"moreColors": [...]
}
]
}
]
}]
I am interested in searching my array for the color value grey and to that object adding a moreColors array moreColors: [{"color" : "blue"}]
. In certain cases this might be a push() method if the array already exists. How would I best achieve this? My goal here is that I want to add values by updating/mutating myData array here because this will be passed on to another function. The nesting here can be several levels deep so a simple loop inside a loop won't work. Would a recursive function work best here? I am also open to better methods or using libraries like underscore or lodash. Although I'd prefer a vanilla js version. Below is a recursive solution I started however, the code won't run more than a level deep.
findNested(myData, "grey")
function findNested(myArray, color) {
myArray.moreColors?.forEach(element => {
if(element.color !== color){
if(element.moreColors.length > 0) {
findNested(element.moreColors, color);
}
} else {
element.moreColors.push({
"color": "blue"
});
}
});
}
CodePudding user response:
Here is a quick example of what I think you are attempting.
findNestedColorObject()
accepts an array of and a color string to search for and returns the first object whose color
property matches.
updateMoreColors()
accepts an object as returned from the above and first assigns moreColors
if it doesn't exist, and then pushes to array.
function findNestedColorObject(array, color) {
let colorObject;
for (const obj of array) {
if (obj.color === color) {
colorObject = obj;
break;
}
if (obj.moreColors !== undefined) {
colorObject = findNestedColorObject(obj.moreColors, color);
}
}
return colorObject;
}
function updateMoreColors(colorObject, colors) {
colorObject.moreColors ??= [];
for (const color of [].concat(colors)) {
colorObject.moreColors.push({ color });
}
}
const myData = [{ "color": "green", "moreColors": [{ "color": "beige" }, { "color": "black", "moreColors": [{ "color": "grey" }, { "color": "white", "moreColors": ["ochre"] }] }] }];
const greyObject = findNestedColorObject(myData, 'grey');
console.log('found:')
console.log(greyObject);
updateMoreColors(greyObject, 'purple');
console.log('updated:');
console.log(greyObject);
const beigeObject = findNestedColorObject(myData, 'beige');
console.log('found:')
console.log(beigeObject);
updateMoreColors(beigeObject, ['salmon', 'crimson']);
console.log('updated:');
console.log(beigeObject);
Or, since your attempt seems to search and update in the same function you can combine the two functions above. (This will continue to update all objects which match, not just the first).
function updateNestedColorObject(array, color, moreColors) {
for (const obj of array) {
if (obj.color === color) {
obj.moreColors ??= [];
for (const color of [].concat(moreColors)) {
obj.moreColors.push({ color });
}
}
if (obj.moreColors !== undefined) {
updateNestedColorObject(obj.moreColors, color, moreColors);
}
}
}
const myData = [{ "color": "green", "moreColors": [{ "color": "beige" }, { "color": "black", "moreColors": [{ "color": "grey" }, { "color": "white", "moreColors": ["ochre"] }] }] }];
updateNestedColorObject(myData, 'grey', 'purple');
updateNestedColorObject(myData, 'purple', 'beige');
updateNestedColorObject(myData, 'beige', ['salmon', 'crimson']);
console.log(myData);