如何计算 javascript 中数组对象的唯一值
How to count unique value from object of array in javascript
我想计算唯一值的数量并将其放入新数组中。
我有以下数组:
[
{ 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 相同,则在新数组中显示计数和新数。
希望你明白我想要什么。
谢谢,
使用 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)
您可以使用 "for..of" 遍历数组并创建一个临时对象来保存每个循环中的数据。如果 tempObject 中存在相同的 id,则将计数递增 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)
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
您可以先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);
}
});
我想计算唯一值的数量并将其放入新数组中。 我有以下数组:
[
{ 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 相同,则在新数组中显示计数和新数。
希望你明白我想要什么。
谢谢,
使用 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)
您可以使用 "for..of" 遍历数组并创建一个临时对象来保存每个循环中的数据。如果 tempObject 中存在相同的 id,则将计数递增 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)
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
您可以先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);
}
});