Javascript 如何计算json对象中不同的属性值
我正在从数据库中获取一个json对象。计算Javascript 如何计算json对象中不同的属性值,javascript,jquery,json,ajax,Javascript,Jquery,Json,Ajax,我正在从数据库中获取一个json对象。计算ClassDef值A、B、C的数量的最佳做法是什么 在例如的json对象中:我在这个json中有“2A、2B和3C” 我的json对象: [{ "ItemCode": "200500303800356", "LastCost": 8, "OnHand": 593889, "InventoryValue": 4751112, "PercOfTotalInventory": 18.294517, "Cumera
ClassDef
值A、B、C
的数量的最佳做法是什么在例如的json对象中:我在这个json中有“2A、2B和3C” 我的json对象:
[{
"ItemCode": "200500303800356",
"LastCost": 8,
"OnHand": 593889,
"InventoryValue": 4751112,
"PercOfTotalInventory": 18.294517,
"CumerativePercTotal": 18.29,
"ClassDef": "A"
}, {
"ItemCode": "201600701800197",
"LastCost": 400,
"OnHand": 300,
"InventoryValue": 120000,
"PercOfTotalInventory": 0.462069,
"CumerativePercTotal": 75.68,
"ClassDef": "A"
}, {
"ItemCode": "200701507000107",
"LastCost": 75,
"OnHand": 239,
"InventoryValue": 17925,
"PercOfTotalInventory": 0.069022,
"CumerativePercTotal": 91.75,
"ClassDef": "B"
}, {
"ItemCode": "200501303400308",
"LastCost": 3515,
"OnHand": 5,
"InventoryValue": 17575,
"PercOfTotalInventory": 0.067674,
"CumerativePercTotal": 91.81,
"ClassDef": "B"
}, {
"ItemCode": "200200106701035",
"LastCost": 80,
"OnHand": 27,
"InventoryValue": 2160,
"PercOfTotalInventory": 0.008317,
"CumerativePercTotal": 99.28,
"ClassDef": "C"
}, {
"ItemCode": "200200902700248",
"LastCost": 10,
"OnHand": 213,
"InventoryValue": 2130,
"PercOfTotalInventory": 0.008202,
"CumerativePercTotal": 99.29,
"ClassDef": "C"
}, {
"ItemCode": "200601302001093",
"LastCost": 0.3,
"OnHand": 6,
"InventoryValue": 1.8,
"PercOfTotalInventory": 0.000007,
"CumerativePercTotal": 100,
"ClassDef": "C"
}]
有人知道怎么做吗?试试这个,看看有多少次
- ClassDef等于C
- ClassDef等于C或B
var-arr=[{ItemCode:“200500303800356”,最后成本:8,现存量:593889,库存价值:4751112,PercoftTotalInventory:18.294517,累计PercTotal:18.29,ClassDef:“A”},{ItemCode:“201600701800197”,最后成本:400,现存量:300,库存价值:12e4,PercoftTotalInventory:462069,累积PercTotal:75.68,ClassDef:“A”},{ItemCode:“200701507000107”,最后成本:75,现存量:239,库存价值:17925,PercoftTotalInventory:.069022,累计PercTotal:91.75,类别定义:“B”},{ItemCode:“200501303400308”,最后成本:3515,现存量:5,库存价值:17575,PercoftTotalInventory:.067674,累计PercTotal:91.81,类别定义:“B”},{ItemCode:“200200106701035”,最后成本:80,现存量:27,存货价值:2160,PercoftTotalInventory:.008317,累计PercTotal:99.28,类别定义:“C”},{ItemCode:“200200902700248”,最后成本:10,现存量:213,存货价值:2130,PercoftTotalInventory:.008202,累计PercTotal:99.29,类别定义:“C”},{ItemCode:“200601302001093”,最后成本:.3,现存量:6,存货价值:1.8,PercoftTotalInventory:7e-6,累计PercTotal:100,类别定义:“C”}];
函数CountOccurrences(名称、值){
var计数=0;
//如果定义了名称和值
if(名称和值){
//检查单个值或值数组
变量值=(数组的值实例?值:[值])
//对于数组中的每个对象
arr.forEach(功能(v){
//如果值数组包含对象[名称]值
if(v[name]&&values.indexOf(v[name])!=-1){
//增量计数器
计数++;
}
});
}
返回计数;
}
log(“ClassDef=='C':”,countoccurrences(“ClassDef”,“C”);
log(“ClassDef==='C'或'B':”,countoccurrences(“ClassDef”、[“C”、“B”))代码>请尝试此操作,以确定执行了多少次
- ClassDef等于C
- ClassDef等于C或B
var-arr=[{ItemCode:“200500303800356”,最后成本:8,现存量:593889,库存价值:4751112,PercoftTotalInventory:18.294517,累计PercTotal:18.29,ClassDef:“A”},{ItemCode:“201600701800197”,最后成本:400,现存量:300,库存价值:12e4,PercoftTotalInventory:462069,累积PercTotal:75.68,ClassDef:“A”},{ItemCode:“200701507000107”,最后成本:75,现存量:239,库存价值:17925,PercoftTotalInventory:.069022,累计PercTotal:91.75,类别定义:“B”},{ItemCode:“200501303400308”,最后成本:3515,现存量:5,库存价值:17575,PercoftTotalInventory:.067674,累计PercTotal:91.81,类别定义:“B”},{ItemCode:“200200106701035”,最后成本:80,现存量:27,存货价值:2160,PercoftTotalInventory:.008317,累计PercTotal:99.28,类别定义:“C”},{ItemCode:“200200902700248”,最后成本:10,现存量:213,存货价值:2130,PercoftTotalInventory:.008202,累计PercTotal:99.29,类别定义:“C”},{ItemCode:“200601302001093”,最后成本:.3,现存量:6,存货价值:1.8,PercoftTotalInventory:7e-6,累计PercTotal:100,类别定义:“C”}];
函数CountOccurrences(名称、值){
var计数=0;
//如果定义了名称和值
if(名称和值){
//检查单个值或值数组
变量值=(数组的值实例?值:[值])
//对于数组中的每个对象
arr.forEach(功能(v){
//如果值数组包含对象[名称]值
if(v[name]&&values.indexOf(v[name])!=-1){
//增量计数器
计数++;
}
});
}
返回计数;
}
log(“ClassDef=='C':”,countoccurrences(“ClassDef”,“C”);
log(“ClassDef==='C'或'B':”,countoccurrences(“ClassDef”、[“C”、“B”))代码>通过一些巧妙的使用,这是小菜一碟
const objArray = [{
"ItemCode": "200500303800356",
"LastCost": 8,
"OnHand": 593889,
"InventoryValue": 4751112,
"PercOfTotalInventory": 18.294517,
"CumerativePercTotal": 18.29,
"ClassDef": "A"
}, ...];
const classDefSet = objArray.reduce((set, obj) => {
set.add(obj.ClassDef); // add value to the set. If it already exists nothing will happen
return set;
}, new Set());
const count = [...classDefSet].length; // convert set to plain array
console.log(count); // 3
通过一些巧妙的使用,这是小菜一碟
const objArray = [{
"ItemCode": "200500303800356",
"LastCost": 8,
"OnHand": 593889,
"InventoryValue": 4751112,
"PercOfTotalInventory": 18.294517,
"CumerativePercTotal": 18.29,
"ClassDef": "A"
}, ...];
const classDefSet = objArray.reduce((set, obj) => {
set.add(obj.ClassDef); // add value to the set. If it already exists nothing will happen
return set;
}, new Set());
const count = [...classDefSet].length; // convert set to plain array
console.log(count); // 3
您可以使用对象作为结果,并使用ClassDef
作为键
var数据=[{ItemCode:“200500303800356”,最后成本:8,现存量:593889,存货价值:4751112,PercoftTotalInventory:18.294517,累计PercTotal:18.29,ClassDef:“A”},{ItemCode:“201600701800197”,最后成本:400,现存量:300,存货价值:120000,PercoftTotalInventory:0.462069,累积PercTotal:75.68,ClassDef:“A”},{项目代码:“200701507000107”,最后成本:75,现存量:239,库存价值:17925,PercoftTotalInventory:0.069022,累计PercTotal:91.75,类别定义:“B”},{项目代码:“200501303400308”,最后成本:3515,现存量:5,库存价值:17575,PercoftTotalInventory:0.067674,累计PercTotal:91.81,类别定义:“B”},{项目代码:“200701106035”,最后成本:80,现存量:27,存货价值:2160,PercoftTotalInventory:0.008317,积数PercTotal:99.28,类别定义:“C”},{ItemCode:“200200902700248”,最后成本:10,现存量:213,存货价值:2130,PercoftTotalInventory:0.008202,积数PercTotal:99.29,类别定义:“C”},{ItemCode:“200601302001093”,最后成本:0.3,现存量:6,存货价值:1.8,PercoftTotalInventory:0.000007,累计PercTotal:100,ClassDef:“C”},
结果=数据。减少(函数(r,o){
r[o.ClassDef]=(r[o.ClassDef]| | 0)+1;
返回r;
},Object.create(null));
console.log(result);
您可以使用对象作为结果,并使用ClassDef
作为键
var数据=[{ItemCode:“200500303800356”,最后成本:8,现存量:593889,存货价值:4751112,PercoftTotalInventory:18.294517,累计PercTotal:18.29,ClassDef:“A”},{ItemCode:“201600701800197”,最后成本:400,现存量:300,存货价值:120000,PercoftTotalInventory:0.462069,累积PercTotal:75.68,ClassDef:“A”},{项目代码:“200701507000107”,最后成本:75,现存量:239,库存价值:17925,PercoftTotalInventory:0.069022,累计PercTotal:91.75,类别定义:“B”},{项目代码:“200501303400308”,最后成本:351
[A: 2, B: 2, C: 3]