4

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!

2
  • Which type of Output you want? Commented Jul 3, 2018 at 11:15
  • I would like the output to an array with objects as the example above . Commented Jul 3, 2018 at 11:19

4 Answers 4

2

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>

Sign up to request clarification or add additional context in comments.

2 Comments

Thanx! I would like to keep the title but then didn't work got "TypeError: b is undefined"
Edited the answer to keep the title in the result @Ali
1

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;
}, []);

Comments

0

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 },
]

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.