亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

如果兩個鍵重復,如何過濾掉對象

如果兩個鍵重復,如何過濾掉對象

慕標琳琳 2022-05-22 18:02:39
const obj  = [       {          "id":"1",          "name":"a",          "email":"[email protected]",          "expiryType":"premium"        },        {          "id":"2",          "name":"b",          "email":"[email protected]",          "expiryType":"gold"        },        {          "id":"3",          "name":"b",          "email":"[email protected]",          "expiryType":"premium"        },    ]有人可以幫我如何過濾掉電子郵件相同但我想保留過期類型為高級的對象嗎?如何使用 Javascript 實現這一點預期輸出為const obj  = [       {          "id":"1",          "name":"a",          "email":"[email protected]",          "expiryType":"premium"        },        {          "id":"3",          "name":"b",          "email":"[email protected]",          "expiryType":"premium"        },    ]
查看完整描述

3 回答

?
小唯快跑啊

TA貢獻1863條經驗 獲得超2個贊

假設您想保留最近一年的條目,您可以保留電子郵件地址和您所見年份的地圖??丛u論:


// The new list

const filtered = [];

// Known emails

const known = new Map();

// Loop through...

for (const entry of obj) {

    // Get this email and expiry

    const {email, expiryYear} = entry;

    // Get the previous info if any

    const previous = known.get(email);

    if (previous) {

        // If the previous one is older than this one,

        // replace it with this one

        if (previous.expiryYear < expiryYear) {

            filtered[previous.index] = entry;

        }

    } else {

        // Add this to the known list and the filtered array

        known.set(email, {

            index: filtered.length,

            expiryYear

        });

        filtered.push(entry);

    }

}

const obj  = [

    {

      "id":"1",

      "name":"a",

      "email":"[email protected]",

      "expiryYear":"2020"

    },

    {

      "id":"2",

      "name":"a",

      "email":"[email protected]",

      "expiryYear":"2019"

    },

    {

      "id":"3",

      "name":"b",

      "email":"[email protected]",

      "expiryYear":"2020"

    },

];


// The new list

const filtered = [];

// Known emails

const known = new Map();

// Loop through...

for (const entry of obj) {

    // Get this email and expiry

    const {email, expiryYear} = entry;

    // Get the previous info if any

    const previous = known.get(email);

    if (previous) {

        // If the previous one is older than this one,

        // replace it with this one

        if (previous.expiryYear < expiryYear) {

            filtered[previous.index] = entry;

        }

    } else {

        // Add this to the known list and the filtered array

        known.set(email, {

            index: filtered.length,

            expiryYear

        });

        filtered.push(entry);

    }

}


console.log(filtered);

這樣做的好處是不會不斷地重新掃描新列表以查找已知條目。



查看完整回答
反對 回復 2022-05-22
?
汪汪一只貓

TA貢獻1898條經驗 獲得超8個贊

您可以根據您想要的唯一鍵過濾掉整個對象,如下所示。


const obj  = 

[

    {

        "id": "1",

        "name": "a",

        "email": "[email protected]",

        "expiryType": "premium"

    },

    {

        "id": "2",

        "name": "b",

        "email": "[email protected]",

        "expiryType": "gold"

    },

    {

        "id": "3",

        "name": "b",

        "email": "[email protected]",

        "expiryType": "premium"

    }

]


function arrayUnique(arr, uniqueKey) {

  const flagList = []

  return arr.filter(function(item) {

    if (flagList.findIndex(flagItem => flagItem[uniqueKey] === item[uniqueKey]) === -1) {

      flagList.push(item)

      return true

    }

  })

}

方法調用....


let newObj = arrayUnique(obj,'email') 

輸出:


newObj = [

    {

        "id": "1",

        "name": "a",

        "email": "[email protected]",

        "expiryType": "premium"

    },

    {

        "id": "3",

        "name": "b",

        "email": "[email protected]",

        "expiryType": "premium"

    }

]


查看完整回答
反對 回復 2022-05-22
?
MYYA

TA貢獻1868條經驗 獲得超4個贊

你可以用 2 個循環簡單地做到這一點。也許不是禁食,而是最簡單的:


function deleteDouble(array, objectKey) {

      for (var i = 0; i < array.length; i++) {

        for (var j = 0; j < array.length; j++) {

          if (i == j) {

            continue;

          }

          if (array[i][objectKey] == array[j][objectKey]) {

            array.splice(i, 1);

            i = 0;

            j = 0;

            break;

          }

        }

      }

      return array;

    }


deleteDouble(obj, "email");


查看完整回答
反對 回復 2022-05-22
  • 3 回答
  • 0 關注
  • 117 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號