在es6中减少对象数组-使用唯一的参数集



我有一个对象数组。在每个对象中都有一个id属性和数组属性rates。我会减少这个对象,这样就没有重复的对象记录。重复记录被定义为唯一id和速率数组。因此,在本例中,id 345出现了两次,并且具有相同的rate数组,因此是重复的。Id 123出现了两次,但速率数组不同,因此不是重复的。

const sampleArray = [
{
"id": "123",
"rates": [{"id": "123", "totalPrice": 100}]
},
{
"id": "345",
"rates": [{"id": "123", "totalPrice": 100}]
},
{
"id": "123",
"rates": [{"id": "123", "totalPrice": 100}, {"id": "456", "totalPrice": 500}]
},
{
"id": "345",
rates": [{"id": "123", "totalPrice": 100}]
}
]

预期输出

const sampleArray = [
{
"id": "123",
"rates": [{"id": "123", "totalPrice": 100}]
},
{
"id": "345",
"rates": [{"id": "123", "totalPrice": 100}]
},
{
"id": "123",
"rates": [{"id": "123", "totalPrice": 100}, {"id": "456", "totalPrice": 500}]
}
]

您可以使用函数Array.prototype.reduce来构建所需的输出,另外,您必须使用函数Array.prototype.everyArray.prototype.some来验证两个速率数组之间的唯一性。

const sampleArray = [    {      "id": "123",      "rates": [{"id": "123", "totalPrice": 100}]    },    {      "id": "345",      "rates": [{"id": "123", "totalPrice": 100}]    },    {      "id": "123",      "rates": [{"id": "123", "totalPrice": 100}, {"id": "456", "totalPrice": 500}]    },    {"id": "345","rates": [{"id": "123", "totalPrice": 100}]}],
result = Object.values(sampleArray.reduce((a, c) => {
let key = `${c.id}_${c.rates.length}`;
if (a[key] && a[key].rates.every((rate) => { 
let rateKeys = Object.keys(rate);
return c.rates.some((innerRate) => rateKeys.every(key => rate[key] === innerRate[key]));
})) {
// The current object "c" is present in the accumulator, because of the following:
//   1. The key (`${c.id}_${c.rates.length}`) already exists within the accumulator.
//   2. All the key-values of object "c" are present in the current object within the accumulator
return a;
} else {
// The current object "c" is not present in the accumulator
return Object.assign(a, {[key]: {...c}});
}
}, {}));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

使用Lodash及其isEqual():

const _ = require('lodash');
function makeUnique( xs ) {
const ys = [];
for ( const x of xs ) {
let equal = false;
for ( const y of ys ) {
equal = _.isEqual(x,y);
if (equal) break;
}
if (!equal) {
ys.push(x);
}
}
return ys;
}
const sampleArray = [
{
id: "123",
rates: [{"id": "123", "totalPrice": 100}]
},
{
id: "345",
rates: [{"id": "123", "totalPrice": 100}]
},
{
id: "123",
rates: [{"id": "123", "totalPrice": 100}, {"id": "456", "totalPrice": 500}]
},
{
id: "345",
rates: [{"id": "123", "totalPrice": 100}]
},
];
const unique = makeUnique(sampleArray);
console.log(JSON.stringify(unique, undefined, 2));

这写

[
{
"id": "123",
"rates": [
{
"id": "123",
"totalPrice": 100
}
]
},
{
"id": "345",
"rates": [
{
"id": "123",
"totalPrice": 100
}
]
},
{
"id": "123",
"rates": [
{
"id": "123",
"totalPrice": 100
},
{
"id": "456",
"totalPrice": 500
}
]
}
]

到控制台。

你可以试试:

const sampleArray = [
{
"id": "123",
"rates": [{"id": "123", "totalPrice": 100}]
},
{
"id": "345",
"rates": [{"id": "123", "totalPrice": 100}]
},
{
"id": "123",
"rates": [{"id": "123", "totalPrice": 100}, {"id": "456", "totalPrice": 500}]
},
{
"id": "345",
"rates": [{"id": "123", "totalPrice": 100}]
}
]
const res = sampleArray.reduce((acc, { id, rates }) => {
if(!(acc.some(item => item.id === id) 
&& (acc.find(item => item.id === id)?.rates.every(r => rates.find(rr => rr.id === r.id && rr.totalPrice === r.totalPrice)))
&& rates?.length === acc.find(item => item.id === id)?.rates.length)) {
acc.push({
id, 
rates
})  
}
return acc;
}, [])
console.log(res);

最新更新