为了账号安全,请及时绑定邮箱和手机立即绑定

数组中对象的比较

数组中对象的比较

ITMISS 2023-09-21 16:32:50
假设我有以下对象数组: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 } ]基本上我的问题是如何找到所有具有相同值的 id,然后比较它们的日期以查看哪个具有更高的值(我计划使用 Date.parse 转换字符串),最后检查哪个具有更大的百分比,以及然后将一个变量分配给条件。不太确定如何去做,但认为它看起来像下面的代码或不是,谢谢您提前的帮助。 for (i = 0; i < myArray.length; i++) {     if (myArray.id[i] === myArray.id[i]) {         if (myArray.date[i] > myArray.date[i]) {            if (myArray.percentage[i] > myArray.percentage[i]) {               let stuff = stuff;           }        }     } }
查看完整描述

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;

    }

}


查看完整回答
反对 回复 2023-09-21
?
哆啦的时光机

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; }


查看完整回答
反对 回复 2023-09-21
  • 2 回答
  • 0 关注
  • 62 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信