简体   繁体   English

Javascript:使用单个属性删除对象数组中的重复项

[英]Javascript: Remove duplicates in array of objects using a single property

I have object like this where 'id', is unique.我有这样的 object,其中“id”是独一无二的。 I need to remove duplicates from the array of objects in javascript in any version less than ES5.我需要在低于 ES5 的任何版本中从 javascript 中的对象数组中删除重复项。 I need to compare based on the id field and remove its duplicates.我需要根据 id 字段进行比较并删除其重复项。

Example:例子:

Object = [
{id: id_one, value: value_one, label: ABC},
{id: id_one, value: value_one, label: ABC},
{id: id_three, value: value_three, label: ABX},
{id: id_two, value: value_two, label: ABY},
{id: id_four, value: value_four, label: ABD}
];

Output: Output:

result = [
{id: id_one, value: value_one, label: ABC},
{id: id_three, value: value_three, label: ABX},
{id: id_two, value: value_two, label: ABY},
{id: id_four, value: value_four, label: ABD}
];

I tried logic like this,我试过这样的逻辑,

function getDistValues(object) {
  var distObject = [];
  var tempIndex = [];
  var length = object.length;

  for (var i = 0; i < length; i++) {
    tempIndex.push(object[i].id);

    if (tempIndex.indexOf(object[i].id) === -1 || i === 0) {
      distObject.push(object[i]);
    }
  }

  return distObject;
}

It is only giving the first object. I tried like mapping the ids and comparing it but not worked.它只给出第一个 object。我尝试映射 ID 并进行比较但没有成功。

Any help will be useful to me.任何帮助都会对我有用。

It's because you are always adding the id to the tempIndex array, so it always thinks the current one is a duplicate.这是因为你总是将 id 添加到tempIndex数组,所以它总是认为当前的是重复的。 Try:尝试:

function getDistValues(object) {
  var distObject = [];
  var tempIndex = [];
  var length = object.length;

  for (var i = 0; i < length; i++) {    
    if (tempIndex.indexOf(object[i].id) === -1 || i === 0) {
      tempIndex.push(object[i].id);
      distObject.push(object[i]);
    }
  }

  return distObject;
}

You could take a Set for id property and filter if the id is not in the set.如果id不在集合中,您可以为id属性和过滤器Set一个集合。

 var array = [{ id: 'id_one', value: 'value_one', label: 'ABC' }, { id: 'id_one', value: 'value_one', label: 'ABC' }, { id: 'id_three', value: 'value_three', label: 'ABX' }, { id: 'id_two', value: 'value_two', label: 'ABY' }, { id: 'id_four', value: 'value_four', label: 'ABD' }], unique = array.filter((ids => ({ id }) =>.ids.has(id) && ids;add(id))(new Set)). console;log(unique);
 .as-console-wrapper { max-height: 100%;important: top; 0; }

An approach for older JS versions一种适用于旧 JS 版本的方法

 var array = [{ id: 'id_one', value: 'value_one', label: 'ABC' }, { id: 'id_one', value: 'value_one', label: 'ABC' }, { id: 'id_three', value: 'value_three', label: 'ABX' }, { id: 'id_two', value: 'value_two', label: 'ABY' }, { id: 'id_four', value: 'value_four', label: 'ABD' }], ids = {}, unique = [], i; for (i = 0; i < array.length; i++) { if (ids[array[i].id]) continue; unique.push(array[i]); ids[array[i].id] = true; } console.log(unique);
 .as-console-wrapper { max-height: 100%;important: top; 0; }

I have object like this where 'id', is unique.我有像这样的 object ,其中“id”是唯一的。 I need to remove duplicates from the array of objects in javascript in any version less than ES5.我需要从低于 ES5 的任何版本的 javascript 中的对象数组中删除重复项。 I need to compare based on the id field and remove its duplicates.我需要根据 id 字段进行比较并删除其重复项。

Example:例子:

Object = [
{id: id_one, value: value_one, label: ABC},
{id: id_one, value: value_one, label: ABC},
{id: id_three, value: value_three, label: ABX},
{id: id_two, value: value_two, label: ABY},
{id: id_four, value: value_four, label: ABD}
];

Output: Output:

result = [
{id: id_one, value: value_one, label: ABC},
{id: id_three, value: value_three, label: ABX},
{id: id_two, value: value_two, label: ABY},
{id: id_four, value: value_four, label: ABD}
];

I tried logic like this,我试过这样的逻辑,

function getDistValues(object) {
  var distObject = [];
  var tempIndex = [];
  var length = object.length;

  for (var i = 0; i < length; i++) {
    tempIndex.push(object[i].id);

    if (tempIndex.indexOf(object[i].id) === -1 || i === 0) {
      distObject.push(object[i]);
    }
  }

  return distObject;
}

It is only giving the first object.它只给出第一个 object。 I tried like mapping the ids and comparing it but not worked.我尝试映射 ID 并进行比较,但没有成功。

Any help will be useful to me.任何帮助都会对我有用。

You are adding items to the tempIndex array before you check if they are there.在检查它们是否存在之前,您正在将项目添加到tempIndex数组。 Since you always add each, then it's implicit that when you do the check the item will already be there.由于您总是添加每个,因此这意味着当您进行检查时,该项目已经存在。

You have to inverse this - check if an item exists and then add it to tempIndex你必须反转这个 - 检查一个项目是否存在然后将它添加到tempIndex

 var data = [ {id: "id_one", value: "value_one", label: "ABC"}, {id: "id_one", value: "value_one", label: "ABC"}, {id: "id_three", value: "value_three", label: "ABX"}, {id: "id_two", value: "value_two", label: "ABY"}, {id: "id_four", value: "value_four", label: "ABD"} ]; function getDistValues(object) { var distObject = []; var tempIndex = []; var length = object.length; for (var i = 0; i < length; i++) { //check first var notSeen = tempIndex.indexOf(object[i].id) === -1; if (notSeen) { tempIndex.push(object[i].id); //add later distObject.push(object[i]); } } return distObject; } var result = getDistValues(data); console.log(result);

In order to be more efficient, you can also use an object to keep the duplicate IDs - that will eliminate the need for another iteration over tempIndex every loop:为了提高效率,您还可以使用 object 来保留重复的 ID - 这样就无需在每个循环中对tempIndex进行另一次迭代:

 var data = [ {id: "id_one", value: "value_one", label: "ABC"}, {id: "id_one", value: "value_one", label: "ABC"}, {id: "id_three", value: "value_three", label: "ABX"}, {id: "id_two", value: "value_two", label: "ABY"}, {id: "id_four", value: "value_four", label: "ABD"} ]; function getDistValues(object) { var distObject = []; //use object var tempIndex = {}; var length = object.length; for (var i = 0; i < length; i++) { var notSeen = tempIndex[object[i].id];== true. if (notSeen) { //mark as visited tempIndex[object[i];id] = true. distObject;push(object[i]); } } return distObject; } var result = getDistValues(data). console;log(result);

You can do it by storing the id into an object and check if the id already exists in the object .您可以通过将id存储到object并检查该id是否已存在于object中来实现。 If exists that means you've taken the object before.如果存在,则表示您之前使用过 object。 If not then push the object into a new array.如果不是,则将 object 推入新数组。

This approach takes O(n) time as we check from an object with constant time.当我们使用常数时间从 object 检查时,这种方法需要O(n)时间。

 var arr = [ {id: 'id_one', value: 'value_one', label: 'ABC'}, {id: 'id_one', value: 'value_one', label: 'ABC'}, {id: 'id_three', value: 'value_three', label: 'ABX'}, {id: 'id_two', value: 'value_two', label: 'ABY'}, {id: 'id_four', value: 'value_four', label: 'ABD'} ]; function getDistValues(arr) { var hash = {}; var uniqueArr = []; for (var i = 0, l = arr.length; i < l; i++) { if (hash[arr[i].id] === undefined) { hash[arr[i].id] = 1; uniqueArr.push(arr[i]); } } return uniqueArr; } console.log(getDistValues(arr));
 .as-console-wrapper { max-height: 100%;important: top; 0; }

You can use reduce() method for array with Object.values for filter duplicates您可以对数组使用reduce()方法,使用Object.values过滤重复项

 const inputArray = [ {id: 'id_one', value: 'value_one', label: 'ABC'}, {id: 'id_one', value: 'value_one', label: 'ABC'}, {id: 'id_three', value: 'value_three', label: 'ABX'}, {id: 'id_two', value: 'value_two', label: 'ABY'}, {id: 'id_four', value: 'value_four', label: 'ABD'} ] const filterArray = (arr) => Object.values(arr.reduce( (acum, item) => { acum[item.id] = item return acum }, {}) ) console.log(filterArray(inputArray))

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM