2 回答
TA贡献2016条经验 获得超9个赞
您需要记住id之前见过的对象,以便可以将它们与每次循环迭代中“现在”查看的对象进行比较。AMap是在现代 JavaScript 中实现这一点的好方法,或者是Object.create(null)在 ES5 中创建的对象。
const lastSeen = new Map();
for (const entry of myArray) {
const {id, date, percentage} = entry;
const last = lastSeen.get(id);
if (last) {
if (date > last.date && percentage > last.percentage) {
// ...this entry is newer than the previous one with the matching ID
// Replace the previous one (and possibly do something with `stuff`?)
lastSeen.set(id, entry);
}
} else {
lastSeen.set(id, entry);
}
}
实例:
const myArray = [ { id: 'first', date: '2020-11-30', percentage: 10 }, { id: 'second', date: '2020-10-30', percentage: 20 }, { id: 'first', date: '2020-09-30', percentage: 30 } ];
const lastSeen = new Map()
for (const entry of myArray) {
const {id, date, percentage} = entry;
const last = lastSeen.get(id);
if (last) {
console.log(`Checking ${id} / ${date} / ${percentage}...`);
if (date > last.date && percentage > last.percentage) {
// ...this entry is newer than the previous one with the matching ID
// Replace the previous one (and possibly do something with `stuff`?)
console.log(`Replacing ${id}...`);
lastSeen.set(id, entry);
} else {
console.log(`Not replacing ${id}`);
}
} else {
console.log(`${id} is new, adding...`);
lastSeen.set(id, entry);
}
}
我没有包含stuff上面的设置,因为不清楚let stuff = stuff;您的原始代码的用途。id您可以找到每个最新的lastSeen或执行上面指示的操作来处理stuff。
在 ES5 级别的代码中(但在 2020 年即将到 2021 年,如果您需要支持过时的环境,我强烈建议编写现代代码并使用转译器):
var lastSeen = Object.create(null);
for (let i = 0; i < myArray.length; ++i) {
var entry = myArray[i];
var last = lastSeen[entry.id];
if (last) {
if (entry.date > last.date && entry.percentage > last.percentage) {
// ...this entry is newer than the previous one with the matching ID
// Replace the previous one (and possibly do something with `stuff`?)
lastSeen[entry.id] = entry;
}
} else {
lastSeen[entry.id] = entry;
}
}
实例:
const myArray = [ { id: 'first', date: '2020-11-30', percentage: 10 }, { id: 'second', date: '2020-10-30', percentage: 20 }, { id: 'first', date: '2020-09-30', percentage: 30 } ];
var lastSeen = Object.create(null);
for (let i = 0; i < myArray.length; ++i) {
var entry = myArray[i];
var last = lastSeen[entry.id];
if (last) {
console.log(`Checking ${entry.id} / ${entry.date} / ${entry.percentage}...`);
if (entry.date > last.date && entry.percentage > last.percentage) {
// ...this entry is newer than the previous one with the matching ID
// Replace the previous one (and possibly do something with `stuff`?)
console.log(`Replacing ${entry.id}...`);
lastSeen[entry.id] = entry;
} else {
console.log(`Not replacing ${entry.id}`);
}
} else {
console.log(`${entry.id} is new, adding...`);
lastSeen[entry.id] = entry;
}
}
TA贡献1779条经验 获得超6个赞
您可以使用对象减少数组,并检查键是否存在或者所需的属性是否更大。
const
data = [{ id: 'first', date: '2020-11-30', percentage: 10 }, { id: 'second', date: '2020-10-30', percentage: 20 }, { id: 'first', date: '2020-09-30', percentage: 30 }],
result = Object.values(data.reduce((r, o) => {
if (
!r[o.id] ||
r[o.id].date < o.date ||
r[o.id].date === o.date && r[o.id].percentage < o.percentage
) {
r[o.id] = o;
}
return r;
}, {}));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
添加回答
举报