Subtract data from an array from another array- Javascript

3

I have the following 2 array:

Array 1:

var datos1=[1,2,3,4,5,6,7];

Array 2:

var datos2=[3,5,7];

What I want is to fill a new array where the data that is not present in var datos2 is saved, for example: [1,2,4,6] , I am trying the following way but it is not as I would like it to be:

 for(var j=0;j<datos1.length;j++){
     for(var k=0;k<datos2.length;k++){
         if(datos1[j]!=datos2[k]){
              datosNoRepetidos[j]=datos2[k];
          }
      }
}

Thank you very much for the help.

    
asked by Baker1562 22.02.2018 в 05:06
source

3 answers

5

I know you got a very well explained and functional answer but this purpose could also be achieved with the method filter to create a new array based on the result that the condition returns. That in the second array not ! this included said iterated element. (el)

var datos1=[1,2,3,4,5,6,7];
var datos2=[3,5,7];
var result = datos1.filter(el => !datos2.includes(el));
console.log(result);
    
answered by 22.02.2018 / 07:05
source
4

let's see ... let's analyze your algorithm ...

The second for has the idea .. but it is not complete .. If you record the value in the data array without repetitions the first time it is not repeated, then you will record all the values of the second array. forever.

That is, you compare the 1 with the 3 .. obviously they are not the same .. then you record the 3. the question would be because?

to start? you should go through the array in reverse order, that is, look for the ones you want to know if they are or not, in the other .. something like this ...

for(var j=0;j<datos2.length;j++){
     for(var k=0;k<datos1.length;k++){
         if(datos1[j]!=datos2[k]){
              datosNoRepetidos[j]=datos2[j];
          }
      }
}

Now, this obviously still does not solve your problem .. because it is missing .. something that says it is not in any place in the second array ...

For that ... let's add a flag ...

for(var j=0;j<datos2.length;j++){
     var esta = false
     for(var k=0;k<datos1.length;k++){
         if(datos2[j]!=datos1[k]){
              datosNoRepetidos[j]=datos2[j];
          }
      }
 }

Let's suppose it is not .. this way .. if it is not .. it will never be true .. to make it true .. we do the following ...

So, let's add an if, that tells us if we add it or not

for(var j=0;j<datos2.length;j++){
     var esta = false
     for(var k=0;k<datos1.length;k++){
         if(datos2[j]!=datos1[k]){
              esta = true
          }
      }
}

If it ever became true ... then it is in some position, and it does not work for us.

for(var j=0;j<datos2.length;j++){
     var esta = false
     for(var k=0;k<datos1.length;k++){
         if(datos2[j]!=datos1[k]){
              esta = true
          }
      }
      if (esta){
          datosNoRepetidos[j]=datos2[j];
      }
}

Now .. we need something more .. dataNoRepetidos [j] is not right .. we have to keep saving in the last position of that array, not in the same position of the array that we are going through .. then .. let's add an accountant.

var pos = 0
for(var j=0;j<datos2.length;j++){
     var esta = false
     for(var k=0;k<datos1.length;k++){
         if(datos2[j]!=datos1[k]){
              esta = true
          }
      }
      if (esta){
          datosNoRepetidos[pos]=datos2[j];
          pos = pos+1
      }
}
    
answered by 22.02.2018 в 05:25
2

You can do it in a simple way with the "union" function of lodash: link

The code would be like that.

let datos1=[1,2,3,4,5,6,7], datos2=[3,5,7];
let resultado = _.union(datos1, datos2);
console.log(resultado);
    
answered by 23.02.2018 в 10:36