简体   繁体   English

计算数组中对象中两个键的出现次数

[英]count occurrences of two keys in objects in array

I have the following array with objects and used the following code to creating a tally with the key "id": 我有以下数组与对象,并使用以下代码创建一个键“id”的计数器:

 var arr=[ { id: 123, title: "name1", status: "FAILED" }, { id: 123, title: "name1", status: "PASSED" }, { id: 123, title: "name1", status: "PASSED" }, { id: 123, title: "name1", status: "PASSED" }, { id: 1234, title: "name2", status: "FAILED" }, { id: 1234, title: "name2", status: "PASSED" } ]; const test =arr.reduce((tally, item) => { if (!tally[item.id]) { tally[item.id] = 1; } else { tally[item.id] = tally[item.id] + 1; } return tally; }, {}); console.log(test); 

Now what I want to do is to modify the tally to take in consideration the key status as well so the result will be somthing like: 现在我想要做的是修改计数以考虑关键状态,所以结果将是这样的:

[
{id:123, status:"PASSED", tally:3},
{id:123, status:"FAILED", tally:1},
{id:1234, status:"PASSED", tally:1},
{id:1234, status:"FAILED", tally:1}
]

Any idea? 任何的想法? Thanks! 谢谢!

Just make the key item.id + item.status , then it's a simple assignment 只需创建关键item.id + item.status ,然后这是一个简单的任务

 const res = Object.values(arr.reduce((a, b) => { a[b.id + b.status] = Object.assign(b, {tally: (a[b.id + b.status] || {tally: 0}).tally + 1}); return a; }, {})); console.log(res); 
 <script> const arr=[ { id: 123, title: "name1", status: "FAILED" }, { id: 123, title: "name1", status: "PASSED" }, { id: 123, title: "name1", status: "PASSED" }, { id: 123, title: "name1", status: "PASSED" }, { id: 1234, title: "name2", status: "FAILED" }, { id: 1234, title: "name2", status: "PASSED" } ]; </script> 

here you go 干得好

const test = arr.reduce((acc, item) => {        
    let found = acc.find(obj => obj.id === item.id && obj.status === item.status)
    if (typeof found === "undefined") {
        item.tally = 1
        acc.push(item);
    } else {
        found.tally++;
    }
    return acc;
}, []);

You should group your items first using key that will contain both id and status: 您应该首先使用包含ID和状态的密钥对项目进行分组:

const result = arr.reduce((acc, item) => {
  const key = item.id + item.status;
  acc[key] = acc[key] || { ...item, tally: 0 };
  acc[key].tally++;
  return acc;
}, {});

console.log( Object.values(result) );

Output: 输出:

[
  { id: 123, title: 'name1', status: 'FAILED', tally: 1 },
  { id: 123, title: 'name1', status: 'PASSED', tally: 3 },
  { id: 1234, title: 'name2', status: 'FAILED', tally: 1 },
  { id: 1234, title: 'name2', status: 'PASSED', tally: 1 },
]

Simply create a key with combination of id and status . 只需创建一个结合了idstatus的密钥。 And make a map using it. 并使用它制作地图。 After that you can simply get the desired result from that. 之后,您可以从中获得所需的结果。 Try the following: 请尝试以下方法:

 var arr=[{id:123,title:"name1",status:"FAILED"},{id:123,title:"name1",status:"PASSED"},{id:123,title:"name1",status:"PASSED"},{id:123,title:"name1",status:"PASSED"},{id:1234,title:"name2",status:"FAILED"},{id:1234,title:"name2",status:"PASSED"}]; const map =arr.reduce((tally, item) => { tally[item.id+"_"+item.status] = (tally[item.id+"_"+item.status] || 0) +1; return tally; }, {}); const result = Object.keys(map).map((a)=>{ var obj = { id : a.split("_")[0], status : a.split("_")[1], tally : map[a] }; return obj; }); console.log(result); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM