1

I want to compare object arrays present in two different React states. My state values are something like below:

review1 = [{name: John, Title: Manager},{name: Peter, Title: Engineer}, {name: Serena, Title: Supervisor}] review2 = [{personName: John, ismanager: yes}, {name: Peter, ismanager: no}, {personName: Serena, ismanager: yes},{personName: John, ismanager:yes}] 

Now I want to get count of people who are manager. If multiple then count should increase. In this case John is manager for two position then count for John is 2 and for Peter 0 and Serena 1

I am trying below function but it doesn't work as expected.

let filter = this.state.review1.map(elem=>elem.personName) count = this.state.review2.map(data=> data.name).reduce((n,x) =>n +(x === filter.personName && data.ismanager === "yes"),null) <div> John: {ismanger.count}, Serena: {ismanager.count}, Peter: {ismanager.count} </div> 

4
  • do you want to count from review1 the managers? Commented Jul 13, 2019 at 17:45
  • I want to count from review2 Commented Jul 13, 2019 at 17:46
  • why not count then from review2? Commented Jul 13, 2019 at 17:47
  • Exactly I am counting it from review2 but don't know how I can do this Commented Jul 13, 2019 at 17:49

2 Answers 2

2

You could count directly the manager with the condition.

var review2 = [{ personName: 'John', ismanager: 'yes' }, { personName: 'Peter', ismanager: 'no' }, { personName: 'Serena', ismanager: 'yes' }, { personName: 'John', ismanager: 'yes' }], count = Array.from( review2.reduce((m, { personName, ismanager }) => m.set(personName, (m.get(personName) || 0) + (ismanager === 'yes')), new Map), ([name, count]) => ({ name, count }) ); console.log(count);

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

7 Comments

it works but if I do have multiple people having managers then it doesn't in my case. Something like below
``` var review2 = [{ personName: 'John', ismanager: 'yes' }, { name: 'Peter', ismanager: 'no' }, ``` {personName:'Serena', ismanager: 'yes'}] count = review2.reduce((s, { ismanager }) => s + (ismanager === 'yes'), 0); console.log(count)
what is the wanted value then?
I want something like separate values for each of the person. Something like count of john is 1. Count of Serena is 1 and Count of Peter is 0
I have updated the post, so yes I have same manager value ismanager="yes" multiple times for few people
|
0

I guess we only need to do it over review2 array right? Try this.

Demo At: playcode

review2 = [{personName: 'John', ismanager: 'yes'}, {name: 'Peter', ismanager: 'no'}, {personName: 'Serena', ismanager: 'yes'},{personName: 'John', ismanager:'yes'}] const countMap = review2.reduce((accum, person) => { const isPersonName = !!person.personName; const name = isPersonName ? person.personName : person.name; if (accum[name] && person.ismanager === 'yes') { accum[name]++; } else if (person.ismanager === 'yes') { accum[name] = 1; } return accum; }, {}) console.log(countMap); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.