0

Is there any way to push elements from array2 if array1 doesn't include them already, without using a for loop iterating each element?

Ex:

array1 = [1, 2, 3]
array2 = [1, 3, 5, 6]

So my result want to be this:

array1 = [1, 2, 3, 5, 6]
chris09trt
  • 91
  • 4
  • search for array merge here on stackoverflow there are plenty of q/a – Diego D Sep 09 '22 at 08:43
  • i dont want just to concat them, i also want to remove duplicates and i was curious if i can do that without concat and filter result after that. i way simpler... – chris09trt Sep 09 '22 at 08:46

1 Answers1

2

array1 = [1, 2, 3]
array2 = [1, 3, 5, 6]

// Use set to remove duplicates, concat to merge the two arrays
result = [...new Set(array1.concat(array2))]

console.log(result)

Remove duplicates

https://www.javascripttutorial.net/array/javascript-remove-duplicates-from-array/

Concat

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat

Wimanicesir
  • 4,606
  • 2
  • 11
  • 30