[英]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: 我想回到我的内部阵列的所有项目
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"
}
]
}
]
So, to get all items inside item
but it's not working: 因此,要使所有项目都包含在
item
但它不起作用:
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
. 或采用即将推出的
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. Array.prototype.filter()
方法创建一个新数组,其中所有元素都通过了由提供的函数实现的测试。 It does not modify the array items. 它不会修改数组项。
You can use Array.prototype.map()
and Array.prototype.flat()
您可以使用
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);
Use Array#reduce with Array#concat like so. 像这样将Array#reduce与Array#concat一起使用。 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.
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.