Different result from compare 2 arrays

How do I compare 2 arrays with the current results:

  1. only the values from a that are not values in b

  2. only the values from b that are not values in a

    var a = [15404300,1567727156,3164959543,92767925,309935495,2759310935,989025462,3109960889,2232537998,67833886,23514543,86720742,837670914,338878286,2598297517,32190488,3292414118,558573564,2813085991,118447616,2380667550];

    var b = [3292414118,2598297517,123];

Current code

for (var i = 0; i < a.length; i++) {
    for (var j = 0; j < b.length; j++) {
        if (a[i] == b[j]) {

        }
        if(b[j] !== a[i]){

        }
    }
}

Solution for JSON objects

var a = [{ data: 15404300, id: 0 },{ data: 1567727156, id: 0 },{ data: 3164959543, id: 0 },{ data: 92767925, id: 0 },{ data: 3181200014, id: 0 },{ data: 3292414118, id: 1 },{ data: 2598297517, id: 2 }];
var b = [{ data: 3292414118, id: 1 },  { data: 2598297517, id: 2 },  { data: 123, id: 3 }];


a.forEach(function(key) { 

    var keyData = key.data;
    if (-1 === b.map(function(d) { return d['data']; }).indexOf(keyData)) {

       res.push(key);
    }

}, this);

Answers


//a-b:
var res = []; // the resulting array

a.forEach(function(key) {
  if (-1 === b.indexOf(key)) {
    res.push(key);
  }
}, this);

For b-a just swap a and b in the example.


Take a look to this.

var resA = [];
a.forEach(function(v,i){ if( b.indexOf(v) < 0) resA.push(v)});
var resB = [];
b.forEach(function(v,i){ if( a.indexOf(v) < 0) resB.push(v)});

or in an more clean way using filter

var resA = a.filter(function(v){ if(b.indexOf(v) < 0) return v ; });
var resB = b.filter(function(v){ if(a.indexOf(v) < 0) return v ; });

Need Your Help

C# CF: file encryption/decryption on the fly

c# encryption compact-framework

i've seen many article on encrypt/decrypt of file and typically a button is used to choose the file for encrypt and another button to decrypt the file.

How to store a phone number in SQL as an integer?

mysql

In MySQL, I built a table using the following code: