1

First let me say am a novice at Javascript so this may be more obvious than I think. But what I'm trying to do is create a new Javascript array from two existing arrays. My thinking is to create a for loop, test if they are equal and if not push that number into a new array. For example.

var allArr = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];
var myArr = [7,8,9,10,14,17];
var fnlArr = [];

What I would like to end up with is fnlArr = [1,2,3,4,5,6,11,12,13,15,16,18,19,20];

Something like:

for (n = 0; n < myArr.length; n++){

    for (p = 0; p < allArr.length; p++){        

        if (p!==myArr[n]){
            fnlArr.push(p);}
     }
}
    

I was also thinking maybe to grep to see if they are the same??? Any help would be appreciated.

dnest007
  • 35
  • 7

1 Answers1

1

You can use .filter:

const allArr = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];
const myArr = [7,8,9,10,14,17];

const set = new Set(myArr);

const fnlArr = allArr.filter(e => !set.has(e));

console.log(fnlArr);
Majed Badawi
  • 27,616
  • 4
  • 25
  • 48