简体   繁体   中英

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":

 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

 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:

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 . 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); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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