-2

I have an array

Persons:[ 
{
Name: 'xz',
Job:'abc',
Manager:true},
{
Name: 'xy',
Job:'ac',
Manager:false},
{
Name: 'z',
Job:'a',
Manager:true}
]

I want to filter out the objects for which manager is true and set state i.e. Person_new:[] with this data using setState.

Can you help me out with this?

Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129
RB19
  • 169
  • 2
  • 10

2 Answers2

0

Because you have a boolean key in your array of objects, you can use filter to pick out only the Persons with manager set to true like so!

const managers = Persons.filter(person => person.Manager)
null
  • 162
  • 5
0

Use the Array filter method to identify the managers and then use the setState method:

const Persons = [
  { Name: 'xz', Job: 'abc', Manager: true },
  { Name: 'xy', Job: 'ac', Manager: false },
  { Name: 'z', Job: 'a', Manager: true }
];

const managers = Persons.filter(person => person.Manager);

this.setState({ persons: managers });
John Ruddell
  • 25,283
  • 6
  • 57
  • 86
Simon Thiel
  • 2,888
  • 6
  • 24
  • 46