0

I'm trying to return all items of array inside my item, this is my array:

    var data =  [
      {
        "id": 275,
        "nome": "name",
        "item": [
          {
            "idCentro": 2,
            "date": "2018-06-05",
            "tipo": "D"
          },
          {
            "idCentro": 6,
            "date": "2017-06-05",
            "tipo": "G"
          },
          {
            "idCentro": 18,
            "date": "2016-06-05",
            "tipo": "G"
          },
          {
            "idCentro": 29,
            "date": "2019-06-05",
            "tipo": "D"
          }
        ]
      }
    ]

So, to get all items inside item but it's not working:

let listaT = data.filter(item => {
  return{
    idCentro: item.item.idCentro,
    date: item.item.date,
    tipo: item.item.tipo,
  }
})

I would like to get this kind of return:

[
  {
    "idCentro": 2,
    "date": "2018-06-05",
    "tipo": "D"
  },
  {
    "idCentroCusto": 6,
    "date": "2017-06-05",
    "tipo": "G"
  },
  {
    "idCentroCusto": 18,
    "date": "2016-06-05",
    "tipo": "G"
  },
  {
    "idCentroCusto": 29,
    "date": "2019-06-05",
    "tipo": "D"
  }
]

How can I achieve this?

Christophvh
  • 12,586
  • 7
  • 48
  • 70
Zkk
  • 741
  • 13
  • 29
  • `data[0].item`, but what's the point of a having an array with one element? – georg Mar 28 '19 at 13:40
  • @georg There are multiple reasons, e.g., not in control of the data format, generalization so you don't have to check for an array or a single object, etc. As *shown*, there's no point. But people often don't ask what they *need* to know, just what they *think* they need to know. – Dave Newton Mar 28 '19 at 13:46
  • Possible duplicate of [Get first element in array](https://stackoverflow.com/questions/4090491/get-first-element-in-array) – kemicofa ghost Mar 29 '19 at 08:50

4 Answers4

5

If you only want to get the array of items, you only have to do

var array = data[0].item
Peter Lehnhardt
  • 4,375
  • 1
  • 14
  • 33
  • I was thinking that it would be something more "robust" and in a simple line, done! Thank you so much for your help! I – Zkk Mar 28 '19 at 20:20
1

You could flat all item arrays.

 var data =  [{ id: 275, nome: "name", item: [{ idCentro: 2, date: "2018-06-05", tipo: "D" }, { idCentro: 6, date: "2017-06-05", tipo: "G" }, { idCentro: 18, date: "2016-06-05", tipo: "G" }, { idCentro: 29, date: "2019-06-05", tipo: "D" }] }],
    result = data.reduce((r, { item }) => [...r, ...item], []);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Or take the upcoming Array#flatMap.

 var data =  [{ id: 275, nome: "name", item: [{ idCentro: 2, date: "2018-06-05", tipo: "D" }, { idCentro: 6, date: "2017-06-05", tipo: "G" }, { idCentro: 18, date: "2016-06-05", tipo: "G" }, { idCentro: 29, date: "2019-06-05", tipo: "D" }] }],
    result = data.flatMap(({ item }) => item);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

The Array.prototype.filter() method creates a new array with all elements that pass the test implemented by the provided function. It does not modify the array items.

You can use Array.prototype.map() and Array.prototype.flat()

var data =  [
      {
        "id": 275,
        "nome": "name",
        "item": [
          {
            "idCentro": 2,
            "date": "2018-06-05",
            "tipo": "D"
          },
          {
            "idCentro": 6,
            "date": "2017-06-05",
            "tipo": "G"
          },
          {
            "idCentro": 18,
            "date": "2016-06-05",
            "tipo": "G"
          },
          {
            "idCentro": 29,
            "date": "2019-06-05",
            "tipo": "D"
          }
        ]
      }
    ]


let listaT = data.map(({item}) => item).flat();
console.log(listaT);
Mamun
  • 66,969
  • 9
  • 47
  • 59
0

Use Array#reduce with Array#concat like so. This also uses destructuring assignment.

The reduce() method executes a reducer function (that you provide) on each member of the array resulting in a single output value.

const data=[{"id":275,"nome":"name","item":[{"idCentro":2,"date":"2018-06-05","tipo":"D"},{"idCentro":6,"date":"2017-06-05","tipo":"G"},{"idCentro":18,"date":"2016-06-05","tipo":"G"},{"idCentro":29,"date":"2019-06-05","tipo":"D"}]}]
    
 const res = data.reduce((a, {item}) => a.concat(item), []);
 
 console.log(res);
kemicofa ghost
  • 16,349
  • 8
  • 82
  • 131