简体   繁体   中英

How to apply filters to an array of JSON in JavaScript?

I have this result from my controller and want to catch some items. Let´s see some code:

```
[
    {
    "id": 17,
    "corretora_id": 1,
    "user_id": 2,
    "order": null,
    "nome": "Johnny English",
    "telefone": "(11)0070-0070",
    "celular": "(00)70070-0700",
    "email": "j@mi7.uk",
    "obs": "É o homem mais burro do mundo. Mas quer o plano mais caro do Mundo  porque ele é rico",
    "img_lead": "1534353433.png",
    "created_at": "2018-08-15 17:17:14",
    "updated_at": "2018-08-15 21:30:48",
    "atendimentos": [
        {
        "id": 22,
        "lead_id": 17,
        "dataAgendamento": "2018-08-16 09:00:00",
        "user_id": 2,
        "comentarios": "Realmente é o homem mais burro do mundo. QUer um plano  de saúde para agentes secretos em nome de 007.\nPediu pra ligar amanhã",
        "created_at": "2018-08-15 20:48:07",
        "updated_at": "2018-08-15 20:48:07"
        },  
        {
        "id": 27,
        "lead_id": 17,
        "dataAgendamento": "2018-08-17 12:00:00",
        "user_id": 2,
        "comentarios": "Teste",
        "created_at": "2018-08-17 18:49:36",
        "updated_at": "2018-08-17 18:49:36"
        },
        {
        "id": 28,
        "lead_id": 17,
        "dataAgendamento": "2018-08-25 19:34:00",
        "user_id": 2,
        "comentarios": "Esse tem que estar em cima quando reload",
        "created_at": "2018-08-18 14:28:24",
        "updated_at": "2018-08-18 14:28:24"
        }
    ]
},
```

I need to read all items from obj id:17 and the sub items inside "atendimentos" node, like "lead_id" and "dataAgendamento".

How can I do it?

first you need to flatten the array, try this function:

flatten(arr, result = []){
    for (var i = 0, length = arr.length; i < length; i++) {
      var value = arr[i];
      if (Array.isArray(value)) {
        flatten(value, result);
      } else {
        result.push(value);
      }
    }
    return result;
  }

You can do so in following ways:

var a = [];//your array variable

a.filter(key =>key.id===17)[0].atendimentos.filter(key=>key.lead_id===17)
//This will give following 
[ { id: 22,
    lead_id: 17,
    dataAgendamento: '2018-08-16 09:00:00',
    user_id: 2,
    comentarios: 'Realmente é o homem mais burro do mundo. QUer um plano  de saúde para agentes secretos em nome de 007.\nPediu pra ligar amanhã',
    created_at: '2018-08-15 20:48:07',
    updated_at: '2018-08-15 20:48:07' },
{ id: 27,
    lead_id: 17,
    dataAgendamento: '2018-08-17 12:00:00',
    user_id: 2,
    comentarios: 'Teste',
    created_at: '2018-08-17 18:49:36',
    updated_at: '2018-08-17 18:49:36' },
{ id: 28,
    lead_id: 17,
    dataAgendamento: '2018-08-25 19:34:00',
    user_id: 2,
    comentarios: 'Esse tem que estar em cima quando reload',
    created_at: '2018-08-18 14:28:24',
    updated_at: '2018-08-18 14:28:24' } ]

You can do lots of array manipulation with ES5 and ES6. To know more functions follow developer.mozilla.org docs .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM