删除对象数组 Javascript 中的重复项

Remove duplicates in an object array Javascript

本文关键字:对象 数组 Javascript 删除      更新时间:2023-09-26

我有一个对象数组

list = [{x:1,y:2}, {x:3,y:4}, {x:5,y:6}, {x:1,y:2}]

我正在寻找一种有效的方法(如果可能的话O(log(n)))来删除重复项并最终

得到
list = [{x:1,y:2}, {x:3,y:4}, {x:5,y:6}]

我尝试了_.uniq甚至_.contains,但找不到令人满意的解决方案。

谢谢!

编辑:该问题已被确定为另一个问题的副本。我在发布之前看到了这个问题,但它没有回答我的问题,因为它是一个对象数组(而不是一个 2 暗淡的数组,谢谢 Aaron),或者至少另一个问题的解决方案在我的情况下不起作用。

Plain JavaScript (ES2015),使用 Set

const list = [{ x: 1, y: 2 }, { x: 3, y: 4 }, { x: 5, y: 6 }, { x: 1, y: 2 }];
const uniq = new Set(list.map(e => JSON.stringify(e)));
const res = Array.from(uniq).map(e => JSON.parse(e));
document.write(JSON.stringify(res));

尝试使用以下方法:

list = list.filter((elem, index, self) => self.findIndex(
    (t) => {return (t.x === elem.x && t.y === elem.y)}) === index)

Vanilla JS 版本:

const list = [{x:1,y:2}, {x:3,y:4}, {x:5,y:6}, {x:1,y:2}];
function dedupe(arr) {
  return arr.reduce(function(p, c) {
    // create an identifying id from the object values
    var id = [c.x, c.y].join('|');
    // if the id is not found in the temp array
    // add the object to the output array
    // and add the key to the temp array
    if (p.temp.indexOf(id) === -1) {
      p.out.push(c);
      p.temp.push(id);
    }
    return p;
    // return the deduped array
  }, {
    temp: [],
    out: []
  }).out;
}
console.log(dedupe(list));

我会使用带有传播运算符的Arrayr.prototype.reduceArrayr.prototype.some方法的组合。

1.明确的解决方案。基于对数组对象包含的完整了解。

list = list.reduce((r, i) => 
  !r.some(j => i.x === j.x && i.y === j.y) ? [...r, i] : r
, [])

这里我们对比较对象结构有严格的限制:{x: N, y: M}[{x:1, y:2}, {x:1, y:2, z:3}]将被过滤到[{x:1, y:2}].

2.通用解决方案,JSON.stringify()。比较的对象可以具有任意数量的任意属性。

list = list.reduce((r, i) => 
  !r.some(j => JSON.stringify(i) === JSON.stringify(j)) ? [...r, i] : r
, [])

此方法对属性顺序有限制,因此不会筛选[{x:1, y:2}, {y:2, x:1}]

3.通用解决方案,Object.keys()。顺序无关紧要。

list = list.reduce((r, i) => 
  !r.some(j => !Object.keys(i).some(k => i[k] !== j[k])) ? [...r, i] : r
, [])

此方法还有另一个限制:比较的对象必须具有相同的键列表。因此,尽管存在明显的差异,[{x:1, y:2}, {x:1}]仍会被过滤。

4.通用解决方案,Object.keys() + .length

list = list.reduce((r, i) => 
  !r.some(j => Object.keys(i).length === Object.keys(j).length 
    && !Object.keys(i).some(k => i[k] !== j[k])) ? [...r, i] : r
, [])

使用最后一种方法,对象通过键的数量、键本身和键值进行比较。

我创建了一个 Plunker 来玩它。

一个用于 ES6+ 的衬里

如果你想通过x和y找到uniq:

arr.filter((v,i,a)=>a.findIndex(t=>(t.x === v.x && t.y===v.y))===i)

如果要按所有属性查找唯一属性:

arr.filter((v,i,a)=>a.findIndex(t=>(JSON.stringify(t) === JSON.stringify(v)))===i)

以下方法有效:

var a = [{x:1,y:2}, {x:3,y:4}, {x:5,y:6}, {x:1,y:2}];
var b = _.uniq(a, function(v) { 
    return v.x && v.y;
})
console.log(b);  // [ { x: 1, y: 2 }, { x: 3, y: 4 }, { x: 5, y: 6 } ]

在检查是否已经在 O(n) 中的 temorary 对象中后过滤数组。

var list = [{ x: 1, y: 2 }, { x: 3, y: 4 }, { x: 5, y: 6 }, { x: 1, y: 2 }],
    filtered = function (array) {
        var o = {};
        return array.filter(function (a) {
            var k = a.x + '|' + a.y;
            if (!o[k]) {
                o[k] = true;
                return true;
            }
        });
    }(list);
document.write('<pre>' + JSON.stringify(filtered, 0, 4) + '</pre>');

没有库,可以使用任何深度

限度:

  • 必须仅提供stringNumber属性作为哈希对象,否则将得到不一致的结果
/** 
 * Implementation, you can convert this function to the prototype pattern to allow
 * usage like `myArray.unique(...)`
 */ 
function unique(array, f) {
  return Object.values(
    array.reduce((acc, item) => ({ ...acc, [f(item).join(``)]: item }), {})
  );
}
const list = [{ x: 1, y: 2}, {x: 3, y: 4}, { x: 5, y: 6}, { x: 1, y: 2}];
// Usage
const result = unique(list, item => [item.x, item.y]);
// Output: [{ x: 1, y: 2}, {x: 3, y: 4}, { x: 5, y: 6}]
console.log(result); 

代码段示例

// Implementation
function unique(array, f) {
  return Object.values(
    array.reduce((acc, item) => ({ ...acc, [f(item).join(``)]: item }), {})
  );
}
// Your object list
const list = [{ x: 1, y: 2}, {x: 3, y: 4}, { x: 5, y: 6}, { x: 1, y: 2}];
// Usage
const result = unique(list, item => [item.x, item.y]);
// Add result to DOM
document.querySelector(`p`).textContent = JSON.stringify(result, null, 2);
<p></p>

使用下划线的_.uniq和标准JSON.stringify它是一个单行:

var list = [{x:1,y:2}, {x:3,y:4}, {x:5,y:6}, {x:1,y:2}];
var deduped = _.uniq(list, JSON.stringify);
console.log(deduped);
<script src="https://underscorejs.org/underscore-umd-min.js"></script>

但是,这假定始终以相同的顺序指定键。通过改进迭代,即使键的顺序不同,我们也可以使解决方案正常工作。这个问题以及解决方案也适用于涉及JSON.stringify的其他答案。

var list = [{x:1,y:2}, {x:3,y:4}, {x:5,y:6}, {y:2, x:1}];
// Ensure that objects are always stringified
// with the keys in alphabetical order.
function replacer(key, value) {
    if (!_.isObject(value)) return value;
    var sortedKeys = _.keys(value).sort();
    return _.pick(value, sortedKeys);
}
// Create a modified JSON.stringify that always
// uses the above replacer.
var stringify = _.partial(JSON.stringify, _, replacer, null);
var deduped = _.uniq(list, stringify);
console.log(deduped);
<script src="https://underscorejs.org/underscore-umd-min.js"></script>

对于 Lodash 4,请使用 _.uniqBy 而不是 _.uniq

使用 lodash,您可以使用以下单行代码:

 _.uniqBy(list, e => { return e.x && e.y })