Junior — Senior
Updating an object's property in a nested array
livecode
Task condition
Given an array containing objects, which may have nested arrays with similar objects. The depth of nesting is unlimited. Each object has fields name and label, which can appear at any nested level. It is necessary to write a function updateValue that searches for an object with specified name and label values and changes the value of a specified property within it.
const data = [
{
name: 'John',
label: 'Developer',
children: [
{
name: 'Jane',
label: 'Designer',
children: [
{
name: 'Jim',
label: 'Designer',
children: []
}
]
},
{
name: 'Jane',
label: 'Designer',
children: []
}
]
},
{
name: 'Alice',
label: 'Manager',
children: []
}
];
function updateValue(data, name, label, key, value) {
// ...
}
The function should recursively traverse the structure, find the object where name and label match the passed arguments, and assign a new value to the key property. If such an object is absent, the array remains unchanged.