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
4 Answers
Reset to default 5If 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
版权声明:本文标题:javascript - How to return all items inside array of object - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744255048a2597437.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论