-1

Suppose there is an Array that has some objects like below

[{name:"Bruce wayne" age:42 DOA:true},{name: "Dick grayson" age:28 DOA:true},{name:"Jason todd" age:24 DOA:false}]

and I want to get a new Array with Objects which DOA is true (bruce and dick).

Is there any good API or something to do this?

your help is going to be appreciated Thx!

1

1 Answer 1

1

You can simply use Array.prototype.filter:

const data = [{
  name: "Bruce wayne",
  age: 42,
  DOA: true
}, {
  name: "Dick grayson",
  age: 28,
  DOA: true
}, {
  name: "Jason todd",
  age: 24,
  DOA: false
}];

console.log(data.filter(obj => obj.DOA));

Mind that your JSON was also invalid.

4
  • 1
    This is an often repeated duplicate question. We close those in favor of the previous versions so that we don't have a million different answers to the same question. Commented Jul 29, 2021 at 16:07
  • oh then, what if I want to get objects which DOA is false?
    – geo
    Commented Jul 29, 2021 at 16:10
  • @user15508209 obj.DOA === false
    – VLAZ
    Commented Jul 29, 2021 at 16:12
  • @user15508209 - Or obj => !obj.DOA assuming you won't have other falsy values like 0 or "". Commented Jul 29, 2021 at 16:16

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.