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]
2
  • search for array merge here on stackoverflow there are plenty of q/a Commented Sep 9, 2022 at 8: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... Commented Sep 9, 2022 at 8:46

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.