0

I have two arrays that look like this

first array

let arr1 = [
  'FS340089',
  'FS350089',
  'FS360089',
  'FS370089',
]

another array

let arr2 = [
  'FS340089',
  'FS350089',
  'FS360089',
  'FS370089',
  'FS380089',
]

I would like to get FS380089 at the output

Andreas
  • 21,535
  • 7
  • 47
  • 56
  • [What topics can I ask about here?](https://stackoverflow.com/help/on-topic), [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users), [How do I ask and answer homework questions?](https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions) – Andreas Jan 07 '23 at 18:17
  • loop trough second array and search each item in first array. – vanowm Jan 07 '23 at 18:17

3 Answers3

1

Try this:

let arr3 = arr2.filter(x => arr1.indexOf(x) == -1);
console.log(arr3); // ["FS380089"]

It works by iterating through the second array and excluding values that are already in the first.

CoderMuffin
  • 519
  • 1
  • 10
  • 21
1

You can use the find() & includes() methods:

let arr1 = [
  'FS340089',
  'FS350089',
  'FS360089',
  'FS370089',
]

let arr2 = [
  'FS340089',
  'FS350089',
  'FS360089',
  'FS370089',
  'FS380089'
]

let result = arr2.find(x => !arr1.includes(x))
console.log(result)
sonEtLumiere
  • 4,461
  • 3
  • 8
  • 35
0

This solution would resolve your problem by covering the case where the array length are different :

let arr1 = [
  'FS340089',
  'FS350089',
  'FS360089',
  'FS370089',
]

let arr2 = [
  'FS340089',
  'FS350089',
  'FS360089',
  'FS370089',
  'FS380089'
]

let result = arr2.length > arr1.length ? arr2.find(x => !arr1.includes(x)) :  arr1.find(x => !arr2.includes(x))
console.log(result)
Mido elhawy
  • 469
  • 4
  • 11