简体   繁体   中英

How to return all items inside array of object

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?

如果只想获取项目数组,则只需要

var array = data[0].item

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; } 

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); 

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); 

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