How to count unique value from object of array in javascript

后端 未结 4 1216
-上瘾入骨i
-上瘾入骨i 2021-01-21 13:34

I want to calculate number of unique value and put that in new array. I have below array:

[
  { CategoryId: \"b5c3f43f941c\", CategoryName: \"Category 1\", Categ         


        
相关标签:
4条回答
  • 2021-01-21 13:57

    You can sort the array first then count the duplicates. The downside is it will modify the original yourArray due to being sorted, so use with caution.

    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);
      }
    });
    
    0 讨论(0)
  • 2021-01-21 13:59
    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
    
    0 讨论(0)
  • 2021-01-21 14:04

    Use reduce function and inside the callback check an object exist whose CategoryId matches. If matches then update the count or else create a new object with the values and push in the array

    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)

    0 讨论(0)
  • 2021-01-21 14:11

    You can loop through array using "for..of" and create a temporary object to save data in every loop. If same id exists in tempObject then increment count by 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)

    0 讨论(0)
提交回复
热议问题