我有一个场景,我需要复制报价(没有价格),如果价格大于0从下面的json:
const data = [
{
"id": "offer1",
"Identifier": {
"info": "some info for offer1",
"value": "some string for offer1"
},
"Price": {
"Total": 94.30
}
},
{
"id": "offer2",
"Identifier": {
"info": "some info for offer2",
"value": "some string for offer2"
},
"Price": {
"Total": 0.0
}
},
{
"id": "offer3",
"Identifier": {
"info": "some info for offer3",
"value": "some string for offer3"
},
"Price": {
"Total": 48.50
}
}
];
我需要获取所有价格〉0的报价,并将它们放入另一个具有特定格式的数组中,预期结果应如下所示:
const result = [
{
"id":"offer1",
"reference":"offer1",
"Identifier":{
"info":"some info for offer1",
"value":"some string for offer1"
}
},
{
"id":"offer3",
"reference":"offer3",
"Identifier":{
"info":"some info for offer3",
"value":"some string for offer3"
}
}
]
我做的第一步是过滤初始数据数组以排除Total = 0的报价。
const filteredOffers = data.filter(offer => offer.Price.Total > 0);
接下来我该怎么做才能得到预期的结果?谢谢。
5条答案
按热度按时间nvbavucw1#
可以Map新对象。
vyswwuz22#
您可以使用reduce方法来Map这些项,并且只返回价格不等于0的项。
vvppvyoh3#
下一步可以是:
jmo0nnb34#
继续你的努力...
const filteredOffers = data.filter(offer => offer.Price.Total > 0);
你可以把过滤后的Map成这样的形状。
56lgkhnf5#
您可以在
map
和filter
方法的帮助下实现所需的功能,例如: