如何返回对象数组内的所有项目

时间:2019-03-28 13:38:10

标签: javascript arrays json

我正在尝试返回item内的所有数组项,这是我的数组:

    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"
          }
        ]
      }
    ]

因此,要在item内获取所有项目,但它不起作用:

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

我想得到这种回报:

[
  {
    "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"
  }
]

我该如何实现?

4 个答案:

答案 0 :(得分:3)

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

var array = data[0].item

答案 1 :(得分:1)

您可以展平所有项目数组。

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

或者采用即将到来的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; }

答案 2 :(得分:1)

Array.prototype.filter()方法创建一个新数组,其中所有元素都通过了由提供的函数实现的测试。它不会修改数组项。

您可以使用Array.prototype.map()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);

答案 3 :(得分:1)

Array#reduceArray#concat一起使用。这也使用destructuring assignment

  

reduce()方法在数组的每个成员上执行reducer函数(由您提供),从而产生单个输出值。

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