久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何從 JavaScript 數組中刪除重復的對象?

How to remove duplicated OBJECTS from JavaScript array?(如何從 JavaScript 數組中刪除重復的對象?)
本文介紹了如何從 JavaScript 數組中刪除重復的對象?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

從對象數組中刪除重復對象的最佳方法是什么?

What's the best way to remove duplicate objects from array of objects?

來自

var arr = 
    [
        {"name":"Joe", "age":17}, 
        {"name":"Bob", "age":17}, 
        {"name":"Carl", "age": 35},
        {"name":"Bob", "age":35},
        {"name":"Joe", "age":17}, 
    ]

刪除重復項后,預期的結果是

when duplicates removed, the expected result is

res= arr = 
    [
        {"name":"Joe", "age":17}, 
        {"name":"Bob", "age":17}, 
        {"name":"Carl", "age": 35},
        {"name":"Bob", "age":35},
    ]

(5 個對象,1 個重復,4 個左).

(5 objects, 1 duplicate, 4 left).

每個對象的屬性數量是固定的,每個數組的屬性名稱相同.但是,從一個數組到另一個數組,它們可能不僅僅是上面的名稱"和年齡",而是屬性的名稱可以是任何名稱.

The number of properties of each object is fixed, the properties names are the same for each array. However, from array to array they may not be just "name" and "age" as above, but the names of the properties could be any.

@Pointy 請將上述問題中的重復詞視為口頭意義上的重復" - 對象分別具有相同數量的屬性、相同的屬性和相同的屬性值.

@Pointy Please treat the duplicate word in the question above as 'duplicate' in the verbal sense - the object with the same number of properties, the same properties and the same values of that properties respectively.

這不是重復的 從 JavaScript 數組中刪除重復項一個>

推薦答案

你可以使用一個對象來查找,如果一個對象已經被插入或者沒有.

You could use an object for lookup, if an object is alreday inserted or not.

更新以獲取對象的所有屬性并使用鍵的值.如果只使用一些屬性,那么我建議使用帶有相關鍵的數組,例如

Update for getting all properties of the object and use the values for the key. If only some properties should be used for it, then I suggest to use an array with the relavant keys, like

['name', 'age']

并與它一起使用

var key = ['name', 'age'].map(function (k) { return a[k]; }).join('|');

var arr = [{ "name": "Joe", "age": 17 }, { "name": "Bob", "age": 17 }, { "name": "Carl", "age": 35 }, { "name": "Bob", "age": 35 }, { "name": "Joe", "age": 17 }],
    filtered = arr.filter(function (a) {
        var key = Object.keys(a).map(function (k) { return a[k]; }).join('|');
        if (!this[key]) {
            return this[key] = true;
        }
    }, Object.create(null));

console.log(filtered);

這篇關于如何從 JavaScript 數組中刪除重復的對象?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

discord.js v12: How do I await for messages in a DM channel?(discord.js v12:我如何等待 DM 頻道中的消息?)
how to make my bot mention the person who gave that bot command(如何讓我的機器人提及發出該機器人命令的人)
How to fix Must use import to load ES Module discord.js(如何修復必須使用導入來加載 ES 模塊 discord.js)
How to list all members from a specific server?(如何列出來自特定服務器的所有成員?)
Discord bot: Fix ‘FFMPEG not found’(Discord bot:修復“找不到 FFMPEG)
Welcome message when joining discord Server using discord.js(使用 discord.js 加入 discord 服務器時的歡迎消息)
主站蜘蛛池模板: 久久国内精品 | 欧美日韩国产在线观看 | 成人在线视频免费观看 | 久久精品一区二区 | 久久精品99国产精品日本 | 欧美二区乱c黑人 | 国产日韩欧美中文 | 亚洲天堂一区二区 | 日日骚网| 中国黄色毛片视频 | 亚洲第一视频网站 | 免费观看一级毛片视频 | 中文字幕亚洲欧美 | 亚洲精品视频在线 | 亚洲成人激情在线观看 | 国产免费高清 | 在线视频一区二区三区 | 日本在线视频一区二区 | 91人人看 | 亚洲福利一区二区 | 国产欧美性成人精品午夜 | 亚洲国产欧美在线 | 久久久久久久综合色一本 | 欧美中文字幕一区二区三区亚洲 | 午夜激情一区 | 一区二区不卡高清 | 国产一区二区久久 | 精品欧美一区二区中文字幕视频 | 欧美精品福利 | 成人免费一区二区三区视频网站 | 色婷婷在线视频 | 日日摸夜夜添夜夜添精品视频 | 亚洲视频在线一区 | 国内av在线 | 中文字幕亚洲一区 | 久久亚洲国产精品日日av夜夜 | 久久国产精品免费一区二区三区 | 国产精品久久久久999 | 亚洲视频在线免费观看 | 婷婷精品 | 亚洲成人一区 |