JS: How do I remove the contents of an array if those contents exist in another array? ... | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

JS: How do I remove the contents of an array if those contents exist in another array? ...

What I'm trying to do here is to avoid duplicates ... the general idea. arrayMain = ["a", "b", "c"] array2 = ["c", "a"] if (arrayMain includes any of array2, arrayMain.remove those strings/content), so, since that is true, arrayMain would end up beingarrayMain = ["b"] "c" and "a" are removed because those are included in array2. ____ That's mainly what I'm trying to do. But, for this specific code, first I'll need to gather some info that will function as array2. and that info comes from 3 HTML elements' background-image URL value. https://code.sololearn.com/W12rAyJduOjv/#js

20th Apr 2020, 4:47 AM
Ginfio
Ginfio - avatar
3 Answers
20th Apr 2020, 5:56 AM
Ipang
+ 3
Use splice to remove. var a = ["a", "b", "c"]; var b = ["a", "c"]; for(var i = 0; i < a.length; i++) { for(var j = 0; j < b.length; j++) { if (a[i] == b[j]) { a.splice(i, 1); } } } console.log(a)
20th Apr 2020, 5:14 AM
A͢J
A͢J - avatar
+ 1
How can I make the array2 from the backgroundImage of .a.. HTML: class = ‘a’ class = ‘a’ class = ‘a empty’ class = ‘a empty’ class = ‘a’ The three .as that aren’t .empty have backgroundImage property. The .empty don’t have backgroundImage. So, how can I get the three .as’ backgroundImage and make an array with it?
20th Apr 2020, 3:16 PM
Ginfio
Ginfio - avatar