在 JavaScript 中合并重复项并增加数组中每个对象的计数

javascriptweb developmentfront end technologyobject oriented programming更新于 2024/12/20 2:52:17

问题陈述说要合并重复元素并增加数组中存在的每个对象的计数,并在 JavaScript 中实现此程序。

了解问题

要开始编码,我们需要了解 JavaScript 函数的基本功能。JavaScript 中的预定义函数将完成我们一半的任务。合并和消除重复项是可以执行的基本数组优化操作。在本期中,我们将定义一个计数变量,用于计算数组中有多少个条目。

我们将使用 Reduce() 方法遍历数组中的每个项目,通过删除重复项来计数和减少数组的大小。因此,Reduction 方法从初始值开始,并使用回调函数来累积值。

在我们的示例中,使用了具有水果和计数两种品质的各种水果。为了增加其中的计数值,如果它们具有相同的果实值,我们将合并它们。

Input Array = [
   {fruit: "Banana", count: 12},
   {fruit: "Kiwi", count: 10},
   {fruit: "Banana", count: 15},
   {fruit: "Kiwi", count: 9},
]

o/p Array = [
   {fruit: "Banana", count: 27},
   {fruit: "Kiwi", count: 19},
]

算法

以下是该程序的算法:

步骤 1:创建一个数组 mergeAndCount 来存储合并的对象。此步骤将初始化第一个操作以检查重复项。

步骤 2:使用回调函数和空的起始值在水果数组 ([]) 上调用 reduce 方法。

步骤 3:确定 an 数组是否包含与回调函数中的 c 对象同名的对象:否则,将 c 项包含在 an 数组中。如果是,则将 c 对象的计数添加到 a 中已有的项目计数中。

步骤 4:从步骤 4 中的 Reduce 函数返回"a"数组。

步骤 5:将 mergeAndCount 返回到控制台。

示例

// 定义一个函数来检查 3 的幂
const fruits = [
   {fruit: "Apple", count: 12},
   {fruit: "Orange", count: 10},
   {fruit: "Pineapple", count: 5},
   {fruit: "Apple", count: 10},
   {fruit: "Orange", count: 4},
   {fruit: "Pineapple", count: 6},
   {fruit: "Banana", count: 12},
   {fruit: "Pineapple", count: 3}
   ]

const mergeAndCount = fruits.reduce((a, c) => {
   const obj = a.find((obj) => obj.fruit === c.fruit);
   if(!obj){
      a.push(c);
   }
   else{
      obj.count += c.count;
   }
   return a;
}, []);

console.log("After counting and merging:");
console.log(mergeAndCount);

输出

After counting and merging:
[
  { fruit: 'Apple', count: 22 },
  { fruit: 'Orange', count: 14 },
  { fruit: 'Pineapple', count: 14 },
  { fruit: 'Banana', count: 12 }
]

示例

// 定义一个数组
const data = [
  { name: 'apple', category: 'fruit' },
  { name: 'orange', category: 'fruit' },
  { name: 'banana', category: 'fruit' },
  { name: 'pear', category: 'fruit' },
  { name: 'apple', category: 'fruit' },
  { name: 'broccoli', category: 'vegetable' },
  { name: 'carrot', category: 'vegetable' },
  { name: 'spinach', category: 'vegetable' },
  { name: 'spinach', category: 'vegetable' },
  { name: 'spinach', category: 'vegetable' }
];

// 创建一个函数来合并和计数
function mergeDuplicates(data, propToMerge) {
  let counts = {};
  for (let obj of data) {
   let propValue = obj[propToMerge];
   if (propValue in counts) {
     counts[propValue]++;
   } else {
     counts[propValue] = 1;
     counts[propValue + '_data'] = [obj];
   }
  }

  let result = [];
  for (let propValue in counts) {
   if (counts[propValue] > 1 && propValue !== propValue + 
'_data') {
     result.push({ [propToMerge]: propValue, count: counts[propValue], 
data: counts[propValue + '_data'] });
   }
  }

  return result;
}

// 调用 mergeDuplicates


const result = mergeDuplicates(data, 'name');
console.log(result);

输出

[
  { name: 'apple', count: 2, data: [ [Object] ] },
  { name: 'spinach', count: 3, data: [ [Object] ] }
]

时间复杂度

上述代码的时间复杂度为 O(n)。因为完成执行所需的时间等于数组的长度。而空间复杂度也是 O(n),用于在数组中存储 n 个元素。

结论

这是解决此类问题的基本思路。在整个过程中,我们使用了名为 mergeAndCount() 的函数、算术运算符和比较运算符来解决问题。并了解了如何计算算法的时间和空间复杂度。


相关文章


有用资源