【问题标题】:How to count unique value from object of array in javascript如何在javascript中计算数组对象的唯一值
【发布时间】:2018-11-13 11:32:04
【问题描述】:

我想计算唯一值的数量并将其放入新数组中。 我有以下数组:

[
  { CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
  { CategoryId: "9872cce5af92", CategoryName: "Category 2", CategoryColor: "purple" }
  { CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
]

我想要具有以下结果的新数组:

[
    { CategoryId: "b5c3f43f941c", count: 2, CategoryColor: "cgreen" }
    { CategoryId: "9872cce5af92", count: 1, CategoryColor: "purple" }
]

在这个通过 id 的检查中,如果 id 是相同的显示计数并且在新数组中是新的。

希望你明白我想要什么。

谢谢,

【问题讨论】:

    标签: javascript arrays array-push


    【解决方案1】:

    您可以使用“for..of”循环遍历数组并创建一个临时对象以在每个循环中保存数据。如果 tempObject 中存在相同的 id,则将 count 增加 1

    var arr = [
      { CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
      , { CategoryId: "9872cce5af92", CategoryName: "Category 2", CategoryColor: "purple" }
      , { CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
    ]
    
    var tempResult = {}
    
    for(let { CategoryColor, CategoryId } of arr)
      tempResult[CategoryId] = { 
          CategoryId, 
          CategoryColor, 
          count: tempResult[CategoryId] ? tempResult[CategoryId].count + 1 : 1
      }      
    
    let result = Object.values(tempResult)
    
    console.log(result)

    【讨论】:

      【解决方案2】:

      使用reduce 函数并在回调中检查是否存在与CategoryId 匹配的对象。如果匹配,则更新计数,否则使用值创建一个新对象并推入数组

      let k = [{
          CategoryId: "b5c3f43f941c",
          CategoryName: "Category 1",
          CategoryColor: "cgreen"
        },
        {
          CategoryId: "9872cce5af92",
          CategoryName: "Category 2",
          CategoryColor: "purple"
        },
        {
          CategoryId: "b5c3f43f941c",
          CategoryName: "Category 1",
          CategoryColor: "cgreen"
        }
      ]
      
      let result = k.reduce(function(acc, curr) {
        // Check if there exist an object in empty array whose CategoryId matches
        let isElemExist = acc.findIndex(function(item) {
          return item.CategoryId === curr.CategoryId;
        })
        if (isElemExist === -1) {
          let obj = {};
          obj.CategoryId = curr.CategoryId;
          obj.count = 1;
          obj.CategoryColor = curr.CategoryColor;
          acc.push(obj)
        } else {
          acc[isElemExist].count += 1
        }
        return acc;
      
      }, [])
      
      console.log(result)

      【讨论】:

        【解决方案3】:
        const array_unique = require('array-hyper-unique').array_unique
        
        let arr = [
          { CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
          { CategoryId: "9872cce5af92", CategoryName: "Category 2", CategoryColor: "purple" }
          { CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
        ]
        
        array_unique(arr).length
        

        【讨论】:

          【解决方案4】:

          您可以先sort 数组,然后计算重复项。缺点是会修改原来的yourArray,因为是排序的,所以慎用。

          yourArray.sort((a, b) => {
            if (a.CategoryName > b.CategoryName) {
              return 1;
            }
            if (a.CategoryName < b.CategoryName) {
              return -1;
            }
            return 0;
          });
          
          var pivot = yourArray[0];
          pivot.count = 0;
          var counted = [pivot];
          yourArray.forEach(item => {
            if (item.CategoryId === pivot.CategoryId) {
              pivot.count++;
            } else {
              pivot = item;
              pivot.count = 1;
              counted.push(pivot);
            }
          });
          

          【讨论】:

            猜你喜欢
            • 2021-01-02
            • 1970-01-01
            • 2021-10-26
            • 1970-01-01
            • 2022-11-15
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-01-03
            相关资源
            最近更新 更多