我正在尝试过滤重复值并将唯一值作为对象数组获取。我不知道如何根据颜色获得唯一值。所以下面是我的数据:
[
{
"code": "xxxx1",
"priceData": {
"currencyIso": "USD",
"value": 649.99
},
"variants": [
{
"color": "#212028 |Black",
}
]
},
{
"code": "xx2",
"priceData": {
"currencyIso": "USD",
"value": 999.99
},
"variants": [
{
"color": "#212028 |Black",
},
]
},
{
"code": "xx3",
"priceData": {
"currencyIso": "USD",
"value": 549.99
},
"variants": [
{
"color": "#D3CCC1 |Silver",
},
]
},
{
"code": "xxx-4",
"priceData": {
"currencyIso": "USD",
"value": 649.99
},
"variants": [
{
"color": "#D3CCC1 |Silver",
}
]
}
]
期望值为:
[
{
"code": "xxxx1",
"priceData": {
"currencyIso": "USD",
"value": 649.99
},
"variants": [
{
"color": "#212028 |Black",
}
]
},
{
"code": "xx3",
"priceData": {
"currencyIso": "USD",
"value": 549.99
},
"variants": [
{
"color": "#D3CCC1 |Silver",
},
]
},
]
下面的代码仅返回对象的变量数组。但我希望作为我上面提到的预期结果
let variants2 = Array.from(
new Set(
variants.map(
(a) => a.variants
)
)
).map((variants) => {
return variants.find((a) => a.color=== a.color);
});
[
...new Map(variants2.map((item) => [item.value, item])).values(),
];
有人可以帮我解决这个问题吗?