user3142695
user3142695

Reputation: 17352

Check for item of multiple object arrays

Assuming this is my array data

[
  { target: ['foo', 'bar'] },
  { target: [] }
]

I need to check if there is minimum of one item for any target array. So in the above example the result should be true.

For this, the result is false:

[
  { target: [] },
  { target: [] }
]

I'm not quite sure how to face this. So I think I have merge all target elements into one array and check if there is an empty array or not.

If this is the correct way, I just need a hint how to merge all arrays into one.

Upvotes: 1

Views: 49

Answers (1)

Nina Scholz
Nina Scholz

Reputation: 386746

You could use Array#some and check the length property of target property.

var array = [{ target: ['foo', 'bar'] }, { target: [] }];

console.log(array.some(({ target }) => target.length));

Upvotes: 4

Related Questions