a more efficient way to achieve the desired output of this code snippet is by checking if sub array 0 exists, combining it, and then filtering the result based on the key code
.
Your assistance is greatly appreciated!
let array = [
{
id: 1,
text: 'stuff1',
arr0: [
{id:1, code: 'imacode1'},
{id:2, code: 'imacode2'},
]
},
{
id: 2,
text: 'stuff2',
arr0: [
{id:3, code: 'imacode3'},
{id:4, code: 'imacode4'},
]
},
{
id: 3,
text: 'stuff3',
arr0: []
}
]
let arr = [];
for(let i of array){
if(i.arr0.length !== 0){
arr.push(i.arr0)
}
}
arr = arr.flat()
for(let j of arr){
if(j.code === 'imacode2'){
let code = arr.filter(j=>j.code!=='imacode2')
code = code.map(({code}) => code)
console.log(code)
}
}
edit: added code snippet for clarity