从对象数组中根据条件删除元素。

3
让我们把 {"src":"A", "target":"B"} 叫做反向对象,对应于 {"src":"B", "target":"A"}。 作为第一步,我想从数组中移除所有的反向对象。 但是我的代码并没有给我期望的结果。
var reducedArr = [
   {"src":"A", "target":"B", "val":"15"},
   {"src":"A", "target":"C", "val":"11"},
   {"src":"B", "target":"A", "val":"9"},
   {"src":"C", "target":"A", "val":"5"},
   {"src":"C", "target":"B", "val":"18"},
   {"src":"B", "target":"A", "val":"19"}
]

var result= reducedArr

result.forEach(element => {

  r = reducedArr.find(e => {return (e.src == element.target && e.target == element.src)});
  if(r){
    reducedArr = reducedArr.filter(ee => ee !== r)
    console.log(reducedArr)
  }
});

正确的结果应该是:
[
   {"src":"A", "target":"B", "val":"15"},
   {"src":"A", "target":"C", "val":"11"},
   {"src":"C", "target":"B", "val":"18"},
]

一旦我搞清楚了,我想添加一个条件,即在对象和其反转版本之间,我会保留具有“val”最高值的那个。 因此,理想的最终结果将是:

[
   {"src":"A", "target":"C", "val":"11"},
   {"src":"C", "target":"B", "val":"18"},
   {"src":"B", "target":"A", "val":"19"}
]

2个回答

4
reduce() 回调函数中,您可以使用 findIndex() 检查是否已经累积了相反的元素,并且如果该值更高,则使用 splice() 插入新元素。

const array = [
  {"src":"A", "target":"B", "val":"15"},
  {"src":"A", "target":"C", "val":"11"},
  {"src":"B", "target":"A", "val":"9"},
  {"src":"C", "target":"A", "val":"5"},
  {"src":"C", "target":"B", "val":"18"},
  {"src":"B", "target":"A", "val":"19"}
];

const result = array.reduce((a, v1) => {
  const i = a.findIndex(v2 => v1.src === v2.target && v1.target === v2.src);
  
  if (i < 0) {
    a.push(v1);
  } else if (+a[i].val < +v1.val) {
    a.splice(i, 1, v1);
  }
  
  return a;
}, []);

console.log(result);


1

Robby提供了一个很好的方法。但是,我有一个稍微不同版本的解决方案。

  • 这个解决方案还检查重复项。因此,在上面的情况下,我们确实有具有相同src和target,但具有不同val的项目。例如:
[
  {"src":"B", "target":"A", "val":"9"},
  {"src":"B", "target":"A", "val":"19"}
]
  • 因此,它会检查、比较并提交根据要求首选的数据。

算法:

1. Run through the given array
2. Keep another array for storing the final result
3. Check whether the final result array has something already stored. If not stored, push the item
4. If there are items, then check for the duplicate or reversed item:
   4(i) If there are not duplicate or reverse item found, simply add it to the final result array
   4(ii) If found, then compare the current val and the founded item's val.
        4(ii)(i) If the current val is greater than the founded one, replace it with the current object in the final array
        4(ii)(ii) If not, then keep the current object in the final result array
5. Once the loop is finished, then return the result or console

const reducedArr = [
    {"src":"A", "target":"B", "val":"15"},
    {"src":"A", "target":"C", "val":"11"},
    {"src":"B", "target":"A", "val":"9"},
    {"src":"C", "target":"A", "val":"5"},
    {"src":"C", "target":"B", "val":"18"},
    {"src":"B", "target":"A", "val":"19"}
];

// To store the expected outcome, which is objects having no reversed
// item and having the maximum val in the object compared from their reversed ones
let resultArray = [];

// Looping through every item to add the item into the new array
reducedArr.forEach((item, index) => {
    // Simply push the item
    if(resultArray.length === 0){
        resultArray.push(item);
    }else{
      // Reverse Foundation. Checks for Duplicate items as well
      const reverseFoundDataIndex = resultArray
      .findIndex(data => (data.src === item.src && data.target === item.target) || (data.target === item.src && data.src === item.target));
      
      // If not found, simply push, cos no reverse data is there
      if(reverseFoundDataIndex === -1) resultArray.push(item);
      else{
        // Founding the maximum value via comparing with the resultArray items and the current item in the given array
        if(parseInt(item.val) > parseInt(resultArray[reverseFoundDataIndex].val))
            // If found one, replacing it with the maximum one
            resultArray[reverseFoundDataIndex] = item;
      }
    }
});

// Sorting based upon the val given in the result array, else, you can do only sort as well
console.log(resultArray
.sort((firstItem, secondItem) => firstItem.val - secondItem.val));

重要的关键点:


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接