Home > front end >  Filter nested array and return entire array that has filter value
Filter nested array and return entire array that has filter value

Time:10-29

I have an array that looks like this:

    Array(4) [ {}, {}, {}, {} ]

0: Object { id: 3, nroPedCliente: 44, nomeCliente: "Testando Strapi",  }
​​
created_at: "2021-09-20T16:23:46.323Z"
​​
dataEntrega: "2021-09-20T18:00:00.000Z"
​​
dataOnda: "2021-10-26T20:00:00.000Z"
​​
horaEntregaFim: "16:00:00.000"
​​
horaEntregaIni: "15:00:00.000"
​​
id: 3
​​
items: Array [ {} ]
​​​
0: Object { id: 3, codItem: 17896229800015, descricao: "Coxinha da Asa Resf Trad",  }
​​​​
categoria: "Ave > Resfriado > Coxinha da Asa > Coxinha da Asa"
​​​​
codInterno: "123"
​​​​
codItem: 17896229800015
​​​​
created_at: "2021-10-01T12:42:38.045Z"
​​​​
descricao: "Coxinha da Asa Resf Trad"
​​​​
id: 3
​​​​
obs: " "
​​​​
pedido: 3
​​​​
published_at: "2021-10-06T11:40:50.061Z"
​​​​
qtd: 18
​​​​
qtdPedida: 0.5
​​​​
setor: "pas"
​​​​
status: "Em andamento"
​​​​
updated_at: "2021-10-27T16:48:26.631Z"
​​​​
<prototype>: Object {  }
​​​
length: 1
​​​
<prototype>: Array []
​​
modalidade: "Testando Strapi"
​​
nomeCliente: "Testando Strapi"
​​
nroPedCliente: 44
​​
nroPedido: 44
​​
published_at: "2021-09-22T13:04:17.375Z"
​​
seqCliente: 44
​​
updated_at: "2021-10-26T19:52:34.087Z"
​​
<prototype>: Object {  }

1: Object { id: 4, nroPedCliente: 44, nomeCliente: "testeNode",  }

2: Object { id: 5, nroPedCliente: 1005, nomeCliente: "testeNode",  }

3: Object { id: 6, nroPedCliente: 6561337, nomeCliente: "Julio Lucas",  }

image for better visualization: https://imgur.com/a/fuDrDos

and this is the request I make to get the array:

        fetch('http://172.20.14.---:----/pedidos/', {
          method: 'GET',
          headers: {
            'Authorization': ` Bearer ${t}`,
            'Content-type': 'application/json'
          }

        })
          .then(function (response) {

            return response.json();


          })
          .then(function (jsonResponse) {
            console.log(jsonResponse);
            var DadosPedidos = jsonResponse;
            pedidosArray = jsonResponse.length;

What I intend to do is filter this result, only returning the Orders that have Items w/ "status": "Em andamento". Notice that I don't want to isolate the item from it's parent Array, as I need the whole order (arrays number 0,1,2,3,4) data to insert in HTML.

I tried something like:

    let abertos = DadosPedidos.reduce(function(a,b) { return a.concat(b);  })
   .filter(function(items) { return items.status == 'Em andamento'; });

and:

    DadosPedidos.map((element) => {
  return {...element, items: element.items.filter((subElement) => subElement.status === 'Em andamento)}
})

with no success. What am I missing? How can I achieve the result of returning every array that has an "items" w/ "status": "Em andamento" entirely? Thank you in advance.

Edit.: This is the json data, as requested:

[{"id":3,"nroPedCliente":44,"nomeCliente":"Testando Strapi","seqCliente":44,"dataEntrega":"2021-09-20T18:00:00.000Z","dataOnda":"2021-10-26T20:00:00.000Z","horaEntregaIni":"15:00:00.000","horaEntregaFim":"16:00:00.000","modalidade":"Testando Strapi","nroPedido":44,"published_at":"2021-09-22T13:04:17.375Z","created_at":"2021-09-20T16:23:46.323Z","updated_at":"2021-10-26T19:52:34.087Z","items":[{"id":3,"codItem":17896229800015,"descricao":"Coxinha da Asa Resf Trad","qtd":18,"qtdPedida":0.5,"setor":"pas","obs":" ","status":"Em andamento","pedido":3,"codInterno":"123","categoria":"Ave > Resfriado > Coxinha da Asa > Coxinha da Asa","published_at":"2021-10-06T11:40:50.061Z","created_at":"2021-10-01T12:42:38.045Z","updated_at":"2021-10-27T16:48:26.631Z"}]},{"id":4,"nroPedCliente":44,"nomeCliente":"testeNode","seqCliente":1003,"dataEntrega":"2021-09-03T03:00:00.000Z","dataOnda":"2021-10-26T19:30:00.000Z","horaEntregaIni":"08:00:00.000","horaEntregaFim":"09:00:00.000","modalidade":"testeNode","nroPedido":1003,"published_at":"2021-09-20T16:42:34.768Z","created_at":"2021-09-20T16:42:34.774Z","updated_at":"2021-10-26T19:40:41.643Z","items":[{"id":5,"codItem":1,"descricao":"Manga Tommy","qtd":1,"qtdPedida":1,"setor":"1","obs":"1","status":"Em andamento","pedido":4,"codInterno":"1","categoria":"Fruta > Tropical > Manga > Tommy","published_at":"2021-10-22T14:37:12.307Z","created_at":"2021-10-22T14:37:10.554Z","updated_at":"2021-10-27T16:47:56.904Z"}]},{"id":5,"nroPedCliente":1005,"nomeCliente":"testeNode","seqCliente":1003,"dataEntrega":"2021-09-03T03:00:00.000Z","dataOnda":"2021-10-26T17:00:00.000Z","horaEntregaIni":"08:00:00.000","horaEntregaFim":"09:00:00.000","modalidade":"testeNode","nroPedido":1005,"published_at":"2021-09-23T18:44:25.989Z","created_at":"2021-09-23T18:44:25.993Z","updated_at":"2021-10-26T19:55:11.015Z","items":[{"id":1,"codItem":999,"descricao":"Picanha Bov Resfriada Montana","qtd":333,"qtdPedida":1.63,"setor":"flv","obs":" ","status":"Em andamento","pedido":5,"codInterno":"999","categoria":"Bovino > Resfriado > Picanha > Peça","published_at":"2021-09-20T16:25:47.729Z","created_at":"2021-09-20T16:25:46.132Z","updated_at":"2021-10-27T16:48:08.023Z"}]},{"id":6,"nroPedCliente":6561337,"nomeCliente":"Julio Lucas","seqCliente":10194833001166,"dataEntrega":"2021-10-01T15:00:00.000Z","dataOnda":"2021-09-23T17:00:00.000Z","horaEntregaIni":"12:00:00.000","horaEntregaFim":"18:00:00.000","modalidade":"* Modalidade * ","nroPedido":6561337,"published_at":"2021-10-01T12:41:52.430Z","created_at":"2021-10-01T12:41:29.250Z","updated_at":"2021-10-26T19:41:22.232Z","items":[{"id":2,"codItem":1,"descricao":"Manga Tommy","qtd":1,"qtdPedida":0.82,"setor":"1","obs":" ","status":"Em andamento","pedido":6,"codInterno":"1","categoria":"Fruta > Tropical > Manga > Tommy","published_at":"2021-09-30T16:00:40.797Z","created_at":"2021-09-30T16:00:33.979Z","updated_at":"2021-10-27T16:47:43.157Z"},{"id":4,"codItem":17896229800053,"descricao":"Queijo Minas Padrão","qtd":6,"qtdPedida":1.12,"setor":"flv","obs":" ","status":"Em andamento.","pedido":6,"codInterno":"17896229800053","categoria":"Lacticínios > Lácteo > Queijo > Minas Padrão","published_at":"2021-10-01T12:43:30.295Z","created_at":"2021-10-01T12:43:28.185Z","updated_at":"2021-10-26T19:41:22.224Z"}]}]

CodePudding user response:

const log_emAndmento = DadosPedidos.map(({items})=>{
return items
})
const f = log_emAndmento.filter((v)=>{
return v.filter(({status})=>status === 'Em andamento.')
})

Tenta agora aq pra mim foi

CodePudding user response:

const log_emAndmento = DadosPedidos.filter(({status})=> status === 'em 
andamento')

console.log(log_emAndmento)

Isso n daria certo lucas ?

  • Related