使用该对象计数数组中的对象以及该对象的计数
count objects from an array with that object and count of that object
我有这个代码片段。并且想要计算重复的 applicationType 和 applicationType 并将两者都计算在内。
{
applicationType:"Supplemental Marketing Application",
createdBy:"System"
},
{
applicationType:"Supplemental Marketing Application",
createdBy:"System"
},
{
applicationType:"Orphan Application",
createdBy:"System"
},
{
applicationType:"Orpahn Application",
createdBy:"System"
},
{
applicationType:"IND/BB",
createdBy:"System"
}
我还想计算应用程序类型以及应用程序类型名称,并希望得到这样的结果,
{
applicationType:"Supplemental Marketing Application",
count:"2"
},
{
applicationType:"Orphan Application",
count:"2"
},
{
applicationType:"IND/BB",
count:"1"
}
或
applicationType = [Supplemental Marketing Application,Orphan Application,IND/BB]
count = [2,2,1]
您可以使用 reduce 函数并检查累加器数组中是否存在其值与原始数组中 applicationType
的值匹配的键
如果 findIndex ===-1
然后使用原始数组中的这些值创建一个新对象,然后推入累加器,否则如果它已经存在则更新 count
的值
let orgArray = [{
applicationType: "Supplemental Marketing Application",
createdBy: "System"
},
{
applicationType: "Supplemental Marketing Application",
createdBy: "System"
},
{
applicationType: "Orphan Application",
createdBy: "System"
},
{
applicationType: "Orpahn Application",
createdBy: "System"
},
{
applicationType: "IND/BB",
createdBy: "System"
}
]
let newArray = orgArray.reduce(function(acc, curr) {
let findIndex = acc.findIndex(function(item) {
return item.applicationType === curr.applicationType
});
if (findIndex === -1) {
let newObj = {}
newObj.applicationType = curr.applicationType;
newObj.count = 1
acc.push(newObj)
} else {
acc[findIndex].count += 1
}
return acc;
}, []);
console.log(newArray)
您可以使用 Array.reduce()
创建地图,按应用程序类型分组。检查应用程序类型是否存在于地图中,如果它已经存在于地图中,则将计数更新为 1,否则将其映射为默认计数 1。地图上的 Object.values()
将为您提供所需的输出:
let arr = [ { applicationType:"Supplemental Marketing Application", createdBy:"System" }, { applicationType:"Supplemental Marketing Application", createdBy:"System" }, { applicationType:"Orphan Application", createdBy:"System" }, { applicationType:"Orphan Application", createdBy:"System" }, { applicationType:"IND/BB", createdBy:"System" } ];
let result = Object.values(arr.reduce((a, curr)=>{
a[curr.applicationType] = a[curr.applicationType] || {applicationType : curr.applicationType, count : 0} ;
a[curr.applicationType].count++;
return a;
},{}));
console.log(result);
我有这个代码片段。并且想要计算重复的 applicationType 和 applicationType 并将两者都计算在内。
{
applicationType:"Supplemental Marketing Application",
createdBy:"System"
},
{
applicationType:"Supplemental Marketing Application",
createdBy:"System"
},
{
applicationType:"Orphan Application",
createdBy:"System"
},
{
applicationType:"Orpahn Application",
createdBy:"System"
},
{
applicationType:"IND/BB",
createdBy:"System"
}
我还想计算应用程序类型以及应用程序类型名称,并希望得到这样的结果,
{
applicationType:"Supplemental Marketing Application",
count:"2"
},
{
applicationType:"Orphan Application",
count:"2"
},
{
applicationType:"IND/BB",
count:"1"
}
或
applicationType = [Supplemental Marketing Application,Orphan Application,IND/BB]
count = [2,2,1]
您可以使用 reduce 函数并检查累加器数组中是否存在其值与原始数组中 applicationType
的值匹配的键
如果 findIndex ===-1
然后使用原始数组中的这些值创建一个新对象,然后推入累加器,否则如果它已经存在则更新 count
let orgArray = [{
applicationType: "Supplemental Marketing Application",
createdBy: "System"
},
{
applicationType: "Supplemental Marketing Application",
createdBy: "System"
},
{
applicationType: "Orphan Application",
createdBy: "System"
},
{
applicationType: "Orpahn Application",
createdBy: "System"
},
{
applicationType: "IND/BB",
createdBy: "System"
}
]
let newArray = orgArray.reduce(function(acc, curr) {
let findIndex = acc.findIndex(function(item) {
return item.applicationType === curr.applicationType
});
if (findIndex === -1) {
let newObj = {}
newObj.applicationType = curr.applicationType;
newObj.count = 1
acc.push(newObj)
} else {
acc[findIndex].count += 1
}
return acc;
}, []);
console.log(newArray)
您可以使用 Array.reduce()
创建地图,按应用程序类型分组。检查应用程序类型是否存在于地图中,如果它已经存在于地图中,则将计数更新为 1,否则将其映射为默认计数 1。地图上的 Object.values()
将为您提供所需的输出:
let arr = [ { applicationType:"Supplemental Marketing Application", createdBy:"System" }, { applicationType:"Supplemental Marketing Application", createdBy:"System" }, { applicationType:"Orphan Application", createdBy:"System" }, { applicationType:"Orphan Application", createdBy:"System" }, { applicationType:"IND/BB", createdBy:"System" } ];
let result = Object.values(arr.reduce((a, curr)=>{
a[curr.applicationType] = a[curr.applicationType] || {applicationType : curr.applicationType, count : 0} ;
a[curr.applicationType].count++;
return a;
},{}));
console.log(result);