N
First, you need to establish a scheme of what you want to do:You have one array (in the answer, I will call that array of main array).That's it. array has several objects inside.These objects have a property c, containing a array of objects (I will call those arrays Secondary arrays).The last array, you want to remove the objects that have the property req with the value teste.You can solve this in many ways. In this answer, we will address the solution using the methods https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Global_Objects/Array/map and https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Global_Objects/Array/filter , both present in https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Global_Objects/Array/prototype of https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Global_Objects/Array .How are we most interested in objects present in main array, we will use the method map to iterate about each object of this main array, performing some changes. From these changes, the map will return new array. Inside the scope of callback the map, we may modify each child object of main array, can filter the array present at the property c, using the method filter.Like this (read comments for a better understanding):const objs = [{
a: 'a',
b: 1,
c: [
{ send: 'send', req: 'req', res: 'res' },
{ send: 'send', req: 'teste', res: 'res' }
]
}, {
a: 'a',
b: 2,
c: [
{ send: 'send', req: 'req', res: 'res' },
{ send: 'send', req: 'teste', res: 'res' }
]
}]
// Usando o método map, vamos iterar sobre cada objeto do array principal, de
// modo em que seremos capazes de modificar a propriedade (array) c, filtrando
// os objetos desse array secundário que possuirem a propriedade req definida
// como "teste":
const filteredObjs = objs.map((obj) => {
// Modificamos somente a propriedade c, atribuindo a ela o array secundário
// filtrado, sem os objetos que possuem req definido como "teste".
//
// O funcionamento é simples: quando retornamos false, o filter remove o
// item da iteração atual. Quando true é retornado, o item é mantido.
obj.c = obj.c.filter((childObj) => {
if (childObj.req === 'teste') {
// Retornaremos false somente quando req for definido como teste,
// removendo esse objeto do array.
return false
}
// Para todos os demais, retorne `true`, mantendo-os no array:
return true
})
// Após filtrarmos a propriedade c, devemos retornar o objeto modificado
// para o método map, caso contrário, ele irá criar um array de valores
// undefined:
return obj
})
console.log(filteredObjs)Note that the above resolution was very verbal. Making full use of the new features brought by versions superior to ES5, we can rewrite the solution like this:const objs = [{
a: 'a',
b: 1,
c: [
{ send: 'send', req: 'req', res: 'res' },
{ send: 'send', req: 'teste', res: 'res' }
]
}, {
a: 'a',
b: 2,
c: [
{ send: 'send', req: 'req', res: 'res' },
{ send: 'send', req: 'teste', res: 'res' }
]
}]
const filteredObjs = objs.map((obj) => ({
...obj,
c: obj.c.filter(({ req }) => req !== 'teste')
}))
console.log(filteredObjs)Note that for didactic purposes it is more prudent than the first code excerpt is taken into account.It is important to note that, for a good understanding of this answer, you are slightly familiar with the operation methods https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Global_Objects/Array/map and https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Global_Objects/Array/filter .