简体   繁体   English

如何从javascript中的对象数组中获取唯一对象

[英]How to get unique objects from objects array in javascript

I have an array of objects that looks like the image below.我有一组类似于下图的对象。 Is there a way by which I can have an array that contains unique objects with respect to id ?有没有办法让我可以拥有一个包含关于id 的唯一对象的数组? We can see below that the id are same at index [0] and index [2].我们可以在下面看到索引 [0] 和索引 [2] 处的id相同。

Is there a way that I can get an array containing objects with unique id and the first object from the last index is added to the unique array rather than the first object.有没有一种方法可以让我得到一个包含具有唯一id 的对象的数组,并且将最后一个索引中的第一个对象添加到唯一数组而不是第一个对象中。 In this case, Object at index[2] should be added instead of object at index[0]:在这种情况下,应添加索引 [2] 处的对象而不是索引 [0] 处的对象: 在此处输入图片说明

To get an array of "unique" objects(with last index within the list) for your particular case use the following approach ( Array.forEach , Array.map and Object.keys functions):要为您的特定情况获取“唯一”对象数组(具有列表中的最后一个索引),请使用以下方法( Array.forEachArray.mapObject.keys函数):

// exemplary array of objects (id 'WAew111' occurs twice)
var arr = [{id: 'WAew111', text: "first"}, {id: 'WAew222', text: "b"}, {id: 'WAew111', text: "last"}, {id: 'WAew33', text: "c"}],
    obj = {}, new_arr = [];

// in the end the last unique object will be considered
arr.forEach(function(v){
    obj[v['id']] = v;
});
new_arr = Object.keys(obj).map(function(id) { return obj[id]; });

console.log(JSON.stringify(new_arr, 0, 4));

The output:输出:

[
    {
        "id": "WAew111",
        "text": "last"
    },
    {
        "id": "WAew222",
        "text": "b"
    },
    {
        "id": "WAew33",
        "text": "c"
    }
]

The best way to do this is to modify your data structure into an object itself where each key is one of the IDs:最好的方法是将您的数据结构修改为一个对象本身,其中每个键都是 ID 之一:

{
    "WadWA7WA6WAaWAdWA...": {
        "text": "birla"
    },
    "WadWA...": {
        "test": "ab"
    }
}

and so forth.等等。 If the data comes from a source formatted that way, you can always map the array of results to this format.如果数据来自以这种方式格式化的源,您总是可以将结果数组映射到这种格式。

You could create a hash using the id as the key and keeping the value as the entire object:您可以使用 id 作为键创建一个散列并将值保留为整个对象:

var myHash = new Object();
var i;

for(i = 0; i < yourArray.length; i++) {
    var yourObjId = yourArray[i][id];
    myHash[yourObjId] = yourArray[i];
}

You would be left with a hash myHash containing objects with unique id's (and only the last object of duplicates would be stored)您将得到一个包含具有唯一 ID 的对象的哈希myHash (并且只会存储重复的最后一个对象)

Try this: just add to a new object using id as the key试试这个:只需使用 id 作为键添加到一个新对象

var arr = [{id:'123', text: 'a'}, {id:'234', text: 'b'}, {id:'123', text: 'c'}]; 
var map = new Object();
for(var i in arr){ map[arr[i].id] = arr[i]; }
var newArr = [];
for(var i in map){ newArr.push(map[i]); }

newArr shall contain the 2nd and 3rd object. newArr 应包含第二个和第三个对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从复杂的对象数组中获取唯一的对象数组? - How to get unique an array of objects back from a complex an array of objects? 如何从对象数组中获取具有特定键的唯一值的对象? - How to get objects with unique values for specific keys from array of objects? 从 2 个对象数组中获取唯一对象 - get unique objects from 2 array of objects JavaScript:从一组对象中获取唯一值及其计数? - JavaScript: Get unique values and their counts from an array of objects? 仅从 JavaScript 中的对象数组中获取唯一数据 - Get only unique data from array of objects in JavaScript 如何在对象数组中获取具有唯一值的键? (JavaScript) - How can I get keys with unique values in an array of objects? (JavaScript) 如何使用 javascript 从对象数组中执行添加相同键的对象值并返回唯一对象? - How to perform addition of same keys' values of objects and return unique objects from array of objects using javascript? 如何从一组复杂的对象中获取一组唯一的属性 - How to get an array of unique properties from an array of complex objects 如何从对象数组中的 arrays 获取唯一值数组? - How to get array of unique values from arrays within an array of objects? 从对象数组中获取唯一的字符串 - Get unique strings from array of array of objects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM