【问题标题】:Update the count value in an array更新数组中的计数值
【发布时间】:2021-11-14 03:41:42
【问题描述】:

我有两个对象数组 arr 1 和 arr 2
arr1 = [{fruit: 'apple',count: 0}] arr2 = [{fruit: 'apple',count: 0}]

需要创建接受 arr1 和 arr2 作为参数的函数,如果同样需要根据计数输入更新计数值,则比较水果名称,否则将值推送到数组中

样本输出 1:

输入: arr1 = [{fruit: 'apple',count: 10}] arr2 = [{fruit: 'apple',count: 30},{fruit: 'orange',count: 20}]

输出: [{fruit: 'apple',count: 40},{fruit: 'orange',count: 20}]

样本输出2

输入: arr1 = [{fruit: 'apple',count: 15}] arr2 = [{fruit: 'orange',count: 20}]

输出: [{fruit: 'apple',count: 15},{fruit: 'orange',count: 20}]

我尝试过的代码:

function compareAndUpdateCount(arr1, arr2) {
    for (i = 0; i < arr1.length; i++) {
        if (arr1[i].fruit === arr2[i].fruit) {
            arr1[i].count = arr1[i].count + arr2[i].count;
        } else {
            arr1.push(arr2[i])
        }
    }
}

当数组长度相同时,这很好用,如果它不同,则需要帮助编写优化代码,如果我们有更多数据,还需要降低时间复杂度

【问题讨论】:

  • 您尝试过的代码在哪里?这很简单。
  • 我已添加代码
  • 这是一个典型的案例,纸和笔可以解决您的问题。试着写出或画出这个算法。首先获取您的第一个输出并更改该设计以适合您的第二个输出。

标签: javascript html jquery angular react-native


【解决方案1】:

你的班级需要一个 setter 来计数 创建一个新的对象数组 temparr

        temparr[0] = arr1[0];
        if((arr1[0].fruit).equals(arr2[0].fruit)) {
            temparr[0].setcount(arr1[0].count+arr2[0].count);
        }
        else {
            temparr[1]=arr2[0];
        }

最后你打印 temparr

【讨论】:

    【解决方案2】:

    您可以使用Map 和reduce 轻松实现结果

    function groupObject(arr1, arr2) {
      const map = new Map();
      const temp = [...arr1, ...arr2].reduce((acc, { fruit, count }) => {
        map.has(fruit)
          ? (map.get(fruit).count = map.get(fruit).count + count)
          : map.set(fruit, { fruit, count });
        return acc;
      }, map);
    
      return [...map.values()];
    }
    
    const arr1 = [{ fruit: "apple", count: 15 }];
    const arr2 = [{ fruit: "orange", count: 20 }];
    console.log(groupObject(arr1, arr2));
    
    const arr3 = [{ fruit: "apple", count: 0 }];
    const arr4 = [{ fruit: "apple", count: 0 }];
    console.log(groupObject(arr3, arr4));
    /* This is not a part of answer. It is just to give the output fill height. So IGNORE IT */
    .as-console-wrapper { max-height: 100% !important; top: 0; }

    【讨论】:

      【解决方案3】:
      var result;
      if(arr1.length < 1) {
          result = arr2;
      } else if(arr2.length < 1) {
          result = arr1;
      } else {
      var existing = [];
      var arr3 = [];
          arr1.forEach(function (item, index) {
            arr2.forEach(function (item2, index2) {
              if(item.fruit === item2.fruit) {
                  item.count = item.count + item2.count;
                  existing.push(index2);
              } else {
              arr3.push(item);
              if(!existing.includes(index2)) {
                  arr3.push(item2);
              }        
              }
            });
          });
          result = arr3;
      }
      

      【讨论】:

      • 我已添加的输入,但输出与预期不符const arr1= [{fruit: 'apple', count :10}] const arr2= [{fruit: 'apple', count :30}, {fruit: 'orange', count: 20}] 我得到的输出[{fruit: 'apple', count :40},{fruit: 'apple', count :40}, {fruit: 'orange', count: 20}]
      • 我编辑了我的回复
      【解决方案4】:

      在另一个循环中循环比较所有对象属性,然后使用 if 语句对计数器求和并使用 splice() 删除第二个数组的项

      试试下面的 sn-p:

      var arr1 = [{fruit: 'apple',count: 10}];
      var arr2 = [{fruit: 'apple',count: 30}, {fruit: 'orange',count: 20}, {fruit: 'apple',count: 20}, {fruit: 'banana',count: 20}];
      var counter = 0;
      
      for(obj1 in arr1){
        for(obj2 in arr2){
          if (arr1[obj1].fruit === arr2[obj2].fruit) {
            arr1[obj1].count = arr1[obj1].count + arr2[obj2].count;
            arr2.splice(counter, 1);
            counter++;
          }
        } 
      }
      arr1 = arr1.concat(arr2);
      console.log(arr1)

      【讨论】:

      • 输出应该在一个数组中
      • 在这种情况下,只需 concat() 两个数组。我更新了代码。
      【解决方案5】:

      尝试使用reduce 将水果及其数量分组,然后使用简单的map 来获取所需的数组

      const arr1 = [{fruit: 'apple',count: 10}] , arr2 = [{fruit: 'apple',count: 30},{fruit: 'orange',count: 20}]
      const res = [...arr1, ...arr2].reduce((acc, curr) => {
        acc[curr.fruit]
      ? (acc[curr.fruit].count += curr.count)
      : (acc[curr.fruit] = curr);
        return acc;
      }, {});
      console.log(Object.keys(res).map((item) => res[item]));

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-11-13
        • 2020-11-12
        • 2018-12-07
        • 1970-01-01
        • 1970-01-01
        • 2011-03-02
        • 2021-07-08
        • 1970-01-01
        相关资源
        最近更新 更多