admin管理员组

文章数量:1401849

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?

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?

Share Improve this question edited Mar 28, 2019 at 14:24 Christophvh 13.3k7 gold badges51 silver badges73 bronze badges asked Mar 28, 2019 at 13:38 ZkkZkk 75115 silver badges31 bronze badges 3
  • data[0].item, but what's the point of a having an array with one element? – georg Commented Mar 28, 2019 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 Commented Mar 28, 2019 at 13:46
  • Possible duplicate of Get first element in array – kockburn Commented Mar 29, 2019 at 8:50
Add a ment  | 

4 Answers 4

Reset to default 5

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

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

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

本文标签: javascriptHow to return all items inside array of objectStack Overflow