I want to calculate number of unique value and put that in new array. I have below array:
[
{ CategoryId: \"b5c3f43f941c\", CategoryName: \"Category 1\", Categ
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)