【发布时间】:2019-08-07 03:18:57
【问题描述】:
我正在尝试使用数组创建新对象。我已经尝试过使用 lodash 地图。但我的问题是如何获取特定集合中的 product_ids 并将其放入数组中。
这是对象
[
[
{
"id": "b7079be6-c9ab-4d24-9a1d-38eddde926c2",
"collection": "mortgages",
"product_id": "854174665132787596022"
},
{
"id": "0da12779-6fe9-45d5-afbf-91d2466e5b7e",
"collection": "mortgages",
"product_id": "304285269353822734222"
},
{
"id": "87a137ba-5f66-4e94-8d5d-698dfbaa08ec",
"collection": "mortgages",
"product_id": "-304414504724243127922"
}
],
[
{
"id": "522f4b83-4c5a-4ffe-836d-33a51325d251",
"collection": "carinsurance",
"product_id": "10413803"
},
{
"id": "02b79566-9cf7-48fa-a8d3-eecf951b5a76",
"collection": "carinsurance",
"product_id": "10397803"
}
]
]
我已经尝试过这段代码
map(this.products, (item, key) => {
this.arr.push({
collection: item[0].collection,
product_ids: item.product_id
});
});
我想要这样的结果。
{
"collection": "mortgages",
"product_ids": [
"304285269353822734222",
"854174665132787596022",
"-304414504724243127922"
]
},
{
"collection": "carinsurance",
"product_ids": [
"10413803",
"10397803"
]
}
但我得到了这个结果。有人可以帮我解决这个问题吗?
{
"collection": "mortgages",
"product_ids": undefined
},
{
"collection": "carinsurance",
"product_ids": undefined
}
【问题讨论】:
标签: javascript angular typescript lodash