I'm trying to modify the values in depth in array in nested array and objects but it seems it's making clone of array I tried to do it recursively.
I'm trying to edit each Boolean values to false :
let data = [{ arr: [true, true, false, false], one: { first: [false, true, true, [true, true, false, false, { arr: [true, { tree: { data: { data: { dd: [true, true, false, false, true] } } } }] }]] } }]
// change all Boolean values to false in example
actionOnArray(array) {
array = array.map((ele) => {
typeof ele === "boolean" && (ele = false)
return ele
})
}
findArrayInDepth(array, action) {
if (Array.isArray(array)) {
actionOnArray(array)
for (let i = 0; i < array.length; i++) {
if (Array.isArray(array[i]) || (typeof array === "object" && array !== null)) {
findArrayInDepth(array[i], action)
}
}
} else if (typeof array === "object" && array !== null) {
for (let key in array) {
if (Array.isArray(array) || (typeof array === "object" && array !== null)) {
findArrayInDepth(array[key], action)
}
}
}
}
findArrayInDepth(data)
console.log(data) // will output the same original array