数组中对象的比较

假设我有以下对象数组:


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;

           }

        }

     }

 }


ITMISS
浏览 91回答 2
2回答

慕沐林林

您需要记住id之前见过的对象,以便可以将它们与每次循环迭代中“现在”查看的对象进行比较。AMap是在现代 JavaScript 中实现这一点的好方法,或者是Object.create(null)在 ES5 中创建的对象。const lastSeen = new Map();for (const entry of myArray) {&nbsp; &nbsp; const {id, date, percentage} = entry;&nbsp; &nbsp; const last = lastSeen.get(id);&nbsp; &nbsp; if (last) {&nbsp; &nbsp; &nbsp; &nbsp; if (date > last.date && percentage > last.percentage) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // ...this entry is newer than the previous one with the matching ID&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Replace the previous one (and possibly do something with `stuff`?)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lastSeen.set(id, entry);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; lastSeen.set(id, entry);&nbsp; &nbsp; }}实例: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) {&nbsp; &nbsp; const {id, date, percentage} = entry;&nbsp; &nbsp; const last = lastSeen.get(id);&nbsp; &nbsp; if (last) {&nbsp; &nbsp; &nbsp; &nbsp; console.log(`Checking ${id} / ${date} / ${percentage}...`);&nbsp; &nbsp; &nbsp; &nbsp; if (date > last.date && percentage > last.percentage) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // ...this entry is newer than the previous one with the matching ID&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Replace the previous one (and possibly do something with `stuff`?)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(`Replacing ${id}...`);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lastSeen.set(id, entry);&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(`Not replacing ${id}`);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; console.log(`${id} is new, adding...`);&nbsp; &nbsp; &nbsp; &nbsp; lastSeen.set(id, entry);&nbsp; &nbsp; }}我没有包含stuff上面的设置,因为不清楚let stuff = stuff;您的原始代码的用途。id您可以找到每个最新的lastSeen或执行上面指示的操作来处理stuff。在 ES5 级别的代码中(但在 2020 年即将到 2021 年,如果您需要支持过时的环境,我强烈建议编写现代代码并使用转译器):var lastSeen = Object.create(null);for (let i = 0; i < myArray.length; ++i) {&nbsp; &nbsp; var entry = myArray[i];&nbsp; &nbsp; var last = lastSeen[entry.id];&nbsp; &nbsp; if (last) {&nbsp; &nbsp; &nbsp; &nbsp; if (entry.date > last.date && entry.percentage > last.percentage) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // ...this entry is newer than the previous one with the matching ID&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Replace the previous one (and possibly do something with `stuff`?)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lastSeen[entry.id] = entry;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; lastSeen[entry.id] = entry;&nbsp; &nbsp; }}实例: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) {&nbsp; &nbsp; var entry = myArray[i];&nbsp; &nbsp; var last = lastSeen[entry.id];&nbsp; &nbsp; if (last) {&nbsp; &nbsp; &nbsp; &nbsp; console.log(`Checking ${entry.id} / ${entry.date} / ${entry.percentage}...`);&nbsp; &nbsp; &nbsp; &nbsp; if (entry.date > last.date && entry.percentage > last.percentage) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // ...this entry is newer than the previous one with the matching ID&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Replace the previous one (and possibly do something with `stuff`?)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(`Replacing ${entry.id}...`);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lastSeen[entry.id] = entry;&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(`Not replacing ${entry.id}`);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; console.log(`${entry.id} is new, adding...`);&nbsp; &nbsp; &nbsp; &nbsp; lastSeen[entry.id] = entry;&nbsp; &nbsp; }}

哆啦的时光机

您可以使用对象减少数组,并检查键是否存在或者所需的属性是否更大。const&nbsp; &nbsp; 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 }],&nbsp; &nbsp; result = Object.values(data.reduce((r, o) => {&nbsp; &nbsp; &nbsp; &nbsp; if (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; !r[o.id] ||&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; r[o.id].date < o.date ||&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; r[o.id].date === o.date && r[o.id].percentage < o.percentage&nbsp; &nbsp; &nbsp; &nbsp; ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; r[o.id] = o;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return r;&nbsp; &nbsp; }, {}));&nbsp; &nbsp;console.log(result);.as-console-wrapper { max-height: 100% !important; top: 0; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript