简体   繁体   English

通过对象属性从对象数组中删除少于元素的元素

[英]Remove elements from array of objects those are less than anothers by object property

I have an array of objects: 我有一个对象数组:

[
{
  incoming_number: 1,
  incoming_number_fraction: 0,
  article:'a1'
},
{
  incoming_number: 2,
  incoming_number_fraction: 0,
  article:'a2'
},
{
  incoming_number: 2,
  incoming_number_fraction: 2,
  article:'a3'
},
{
  incoming_number: 3,
  incoming_number_fraction: 0,
  article:'a4'
},
{
  incoming_number: 4,
  incoming_number_fraction: 0,
  article:'a5'
},
{
  incoming_number: 4,
  incoming_number_fraction: 2,
  article:'a6'
},
{
  incoming_number: 4,
  incoming_number_fraction: 4,
  article:'a7'
},
]

The task is removing elements from array by this logic: if there are elements with similar "incoming_number" , we must save the element with max "incoming_number_fraction" and remove all others. 任务是通过此逻辑从数组中删除元素:如果存在具有类似“ incoming_number”的元素,则必须保存最大为“ incoming_number_fraction”的元素,然后删除所有其他元素。
The result should be: 结果应为:

[
{
      incoming_number: 1,
      incoming_number_fraction: 0
},
{
      incoming_number: 2,
      incoming_number_fraction: 2
},
{
      incoming_number: 3,
      incoming_number_fraction: 0
},
{
      incoming_number: 4,
      incoming_number_fraction: 4
},
]  

There are a lot of elements in array (more than 10000) and I want to do this operation in the minimum number of steps. 数组中有很多元素(超过10000个),我想以最少的步骤数执行此操作。

EDIT 编辑
I try something like this: 我尝试这样的事情:

for (let i = 0; i < arr.length ; i++) {  
if (arr[i]['incoming_number'] === arr[i + 1]['incoming_number']) {
  let j = i + 1
  while (arr[j]['incoming_number'] === arr[j + 1]['incoming_number']) {
    j++
  }
}

arr.splice(i, j-1)
}  

But it doesn't work... 但这行不通...
EDIT 2 编辑2
Adding more information about objects in array. 添加有关数组中对象的更多信息。

Using findIndex or grep to find the selected number in a big array is bad for performance. 使用findIndex或grep在大数组中查找选定的数字不利于性能。 so using the number as a property will be the fastest way to find the object and change incoming_number_fraction . 因此,将数字用作属性将是查找对象和更改incoming_number_fraction的最快方法。

Using grep, findindex may result in a samller code but not better performance and in your case you need performance more then smaller code. 使用grep时,findindex可能会导致代码混乱,但性能却不佳,在您的情况下,您需要的性能比较小的代码还要多。

have a look at my example and let me know. 看一下我的例子,让我知道。

 var arr=[ { incoming_number: 1, incoming_number_fraction: 0, article:'a1' }, { incoming_number: 2, incoming_number_fraction: 0, article:'a2' }, { incoming_number: 2, incoming_number_fraction: 2, article:'a3' }, { incoming_number: 3, incoming_number_fraction: 0, article:'a4' }, { incoming_number: 4, incoming_number_fraction: 0, article:'a5' }, { incoming_number: 4, incoming_number_fraction: 2, article:'a6' }, { incoming_number: 4, incoming_number_fraction: 4, article:'a7' }, ] var finalResult = []; var result = {}; var includedNumbers = [] arr.forEach(function(item){ var number = item.incoming_number; if (!result[item.incoming_number]){ // with Index, is the fastest way result[number] = item; includedNumbers.push(number); }else { if (result[number].incoming_number_fraction< item.incoming_number_fraction ){ result[number] = item; } } }); // now lets clean the result includedNumbers.forEach(function(item){ finalResult.push(result[item]) }); console.log(finalResult) 

the easiest way to achieve this is using reduce , because with one iteration you will get your result and you apply your logic directly to the result array. 实现此目的最简单的方法是使用reduce ,因为一次迭代即可获得结果,并将逻辑直接应用于结果数组。

 const obj = [{ incoming_number: 1, incoming_number_fraction: 0 }, { incoming_number: 2, incoming_number_fraction: 0 }, { incoming_number: 2, incoming_number_fraction: 2 }, { incoming_number: 3, incoming_number_fraction: 0 }, { incoming_number: 4, incoming_number_fraction: 0 }, { incoming_number: 4, incoming_number_fraction: 2 }, { incoming_number: 4, incoming_number_fraction: 4 }, ] const result = obj.reduce((accum, currentValue) => { const index = accum.findIndex(item => currentValue.incoming_number === item.incoming_number); //if the index is -1 it means we didn't added that entry, so we just push it. if (index === -1) { accum.push(currentValue); } else { //this means that we have an entry with that incoming number, then we compare the incoming_number_fraction if (currentValue.incoming_number_fraction > accum[index].incoming_number_fraction) { accum[index] = currentValue; } } return accum; }, []) console.log(result) 

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

相关问题 通过 object 属性从数组中删除对象 - remove objects from array by object property 从对象数组中的对象中删除属性 mongoose - remove a property from an object in an array of objects mongoose 如何通过对象的属性从对象数组中删除特定的对象? - How to remove a specific Object from an array of Objects, by object's property? 根据 object 属性的前 3 个字从对象数组中删除重复项 - Remove duplicates from an array of objects based on the first 3 words of object property 按属性值从深度嵌套的 object 数组中删除对象 - Remove objects from a deeply nested object array by property value 从数组中删除一个对象然后更新所有对象的属性 - Remove an object from the array then update the property of all objects 通过 object 属性名从数组中删除非重复对象 - Remove non repeating objects from array by object property name 从 object 数组中移除具有相同类型属性的连续对象 - Remove consecutive objects with the same type property from object array 根据 88222995402888 属性值从 javascript 对象数组中删除重复项 - Remove duplicates from javascript objects array based on object property value 在LARGE排序数组中删除长度小于“n”的元素 - Remove elements having length less than `n`, in a LARGE sorted array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM