+ 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
3 Answers
+ 4
Use `filter`method of Array class.
https://code.sololearn.com/Wnh25j0scJ90/?ref=app
* Details here đ
https://codeburst.io/useful-javascript-array-and-object-methods-6c7971d93230
+ 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)
+ 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?