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

时间:2022-09-25 10:24:05

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 ? We can see below that the id are same at index [0] and index [2].

我有一组对象,如下图所示。有没有办法让我可以拥有一个包含与id相关的唯一对象的数组?我们可以在下面看到,索引[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. In this case, Object at index[2] should be added instead of object at index[0]: 如何在javascript中从对象数组中获取唯一对象

有没有办法可以获得一个包含具有唯一id的对象的数组,并将最后一个索引中的第一个对象添加到唯一数组而不是第一个对象。在这种情况下,应该在索引[0]处添加索引为[2]的对象而不是对象:

4 个解决方案

#1


1  

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.forEach,Array.map和Object.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"
    }
]

#2


0  

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.

等等。如果数据来自以这种方式格式化的源,您始终可以将结果数组映射到此格式。

#3


0  

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)

你将留下一个散列myHash,其中包含具有唯一id的对象(并且只存储重复的最后一个对象)

#4


0  

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应包含第2个和第3个对象。

#1


1  

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.forEach,Array.map和Object.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"
    }
]

#2


0  

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.

等等。如果数据来自以这种方式格式化的源,您始终可以将结果数组映射到此格式。

#3


0  

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)

你将留下一个散列myHash,其中包含具有唯一id的对象(并且只存储重复的最后一个对象)

#4


0  

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应包含第2个和第3个对象。