0

I have following arrays of objects: enter image description here

I need to check if these two arrays have the same begin property. In this case that appears to be true in three objects. So far I have tried this code, but with no luck (input.value being the first array).

const found = input.value.some((item) => item.begin === filtered.filter((time) => time.begin));

Any ideas how can I achieve this? Thank you in advance.

2
  • do you want only the count? please add the wanted result. Commented Nov 16, 2020 at 12:04
  • I want the output to be true/false Commented Nov 16, 2020 at 12:05

2 Answers 2

2

You can use simple map and findIndex method.

const found = input.value.map((item) => filtered.findIndex((time) => time.begin === item.begin) > -1).findIndex((item) => item === true) > -1;
Sign up to request clarification or add additional context in comments.

Comments

2

You could take a Set for the beginnings of filtered and check against the set without more iterations.

Big O of this is O(n), because of O(n) of creating the set and another O(n) of the check and beacause of adding two n you get O(n).

The other approach has because of the nested structure O(n2).

const
    filteredSet = new Set(filtered.map(({ begin }) => begin)),
    found = input.value.some(({ begin }) => filteredSet.has(begin));

1 Comment

This works, I just like the one line solution better. Thank you though.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.