第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

數(shù)組中對象的比較

數(shù)組中對象的比較

ITMISS 2023-09-21 16:32:50
假設我有以下對象數(shù)組: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條經(jīng)驗 獲得超9個贊

您需要記住id之前見過的對象,以便可以將它們與每次循環(huán)迭代中“現(xiàn)在”查看的對象進行比較。AMap是在現(xiàn)代 JavaScript 中實現(xiàn)這一點的好方法,或者是Object.create(null)在 ES5 中創(chuàng)建的對象。


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或執(zhí)行上面指示的操作來處理stuff。


在 ES5 級別的代碼中(但在 2020 年即將到 2021 年,如果您需要支持過時的環(huán)境,我強烈建議編寫現(xiàn)代代碼并使用轉譯器):


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條經(jīng)驗 獲得超6個贊

您可以使用對象減少數(shù)組,并檢查鍵是否存在或者所需的屬性是否更大。


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 關注
  • 131 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號