删除对象数组Javascript中的重复元素 [英] Delete duplicated elements in array of objects Javascript
问题描述
基本上我想知道如何在JavaScript中清除对象数组.
Basically I want to know how to clean an array of objects in JavaScript.
我看过一些示例,这些示例显示了如何根据一个元素的值执行此操作,我需要将其通用.这意味着它不依赖于孩子的名字.
I have seen some samples that show how to do it depending on the value of one element and I need it to be generic. It means that it doesn't depend on the child's names.
我需要的是一个可以独立于对象结构工作的函数.
What I need is a function that can work independently of the object structure.
例如,它可以与此配合使用:
For example, it can work with this:
object = [
{value: 'value', configuration: 'configuration'},
{value: 'value2', configuration: 'configuration2'},
{value: 'value', configuration: 'configuration'},
]
//returning:
object = {
{value: 'value', configuration: 'configuration'},
{value: 'value2', configuration: 'configuration2'}
}
它还可以与:
object = [
{name: 'name', property: 'property'},
{name: 'name2', property: 'property2'},
{name: 'name', property: 'property'},
]
//returning:
object = [
{name: 'name', property: 'property'},
{name: 'name2', property: 'property2'}
]
推荐答案
使用filter
/findIndex
技巧对数组进行重复数据删除非常简单,唯一的问题是我们需要使用相等函数来比较元素.对于对象,简单的===
无效.
Deduplicating an array is straightforward using the filter
/findIndex
trick, the only problem is that we need an equality function to use to compare elements. For objects, a simple ===
won't work.
这是一个使用基本的浅等于比较器的简单示例.它仅检查两个对象的相同键的值是否相同,这将适用于示例中的对象(但不适用于嵌套对象).根据您的用例,可能还不够.另一个不错的选择是lodash的_.isEqual
方法,或者一个比较其JSON字符串的函数. 重要的是,您定义了一个函数来比较两个满足您要求的对象.
Here is a simple example using a basic shallow equals comparator. It only checks that two objects have the same values for the same keys, which will work for the objects in your example (but it wouldn't work with nested objects). Depending on your use case that may not be sufficient. Another good option would be lodash's _.isEqual
method, or a function to compare their JSON strings. The important thing is that you define a function to compare two objects that meets your requirements.
var array1 = [
{value: 'value', configuration: 'configuration'},
{value: 'value2', configuration: 'configuration2'},
{value: 'value', configuration: 'configuration'},
];
var array2 = [
{name: 'name', property: 'property'},
{name: 'name2', property: 'property2'},
{name: 'name', property: 'property'},
];
// You need to define an equality function to use with the deduplicator. Heres a basic one.
function isKVEqual(obj1, obj2) {
// Get the keys of these objects, make sure they have the same number of keys.
const o1keys = Object.keys(obj1);
const o2keys = Object.keys(obj2);
if (o1keys.length !== o2keys.length) return false;
// Check that the value of each key is the same in each object.
for (const key of o1keys) {
if (obj2[key] !== obj1[key]) return false;
}
return true;
}
// Deduplicate:
// Make sure to replace the isKVEqual call with whatever custom comparator you want to use.
const dedupedArray1 = array1.filter((item, index, list) => list.findIndex(x => isKVEqual(x, item)) === index);
const dedupedArray2 = array2.filter((item, index, list) => list.findIndex(x => isKVEqual(x, item)) === index);
console.log(dedupedArray1)
console.log(dedupedArray2)
这篇关于删除对象数组Javascript中的重复元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!