0

I have two array,

var original = [10,80,30,100,160];
var edited = [80,120,140,70,160,30];

from above,

I need to compare and get which all elements are present in array "edited" but not present in "original".

Also,

Need to get elements which were present in "original" but not in "edited".

So the output should be as follows,

Added Elements 120,140,70
Deleted Elements 10,100

The code which I wrote is as follows,

var fn100 = function(){
    var original = [10,80,30,100,160];
    var edited = [80,120,140,70,160,30];
    var newlyAdded = [];
    var dropped = [];
    var isFound = false;
    var x = 0, y = 0;

    //for added
    for(x = 0; x < edited.length; x++){

        isFound = false;

        for(y = 0; y < original.length; y++){
            if(edited[x] === original[y]){
                isFound = true;
                break;
            }
        }

        if(isFound === false){
            newlyAdded.push(edited[x]);
        }
    }//for added

    //for dropped
    for(x = 0; x < original.length; x++){
        isFound = false;
        for(y = 0; y < edited.length; y++){
            if(original[x] === edited[y]){
                isFound = true;
                break;
            }
        }

        if(isFound === false){
            dropped.push(original[x]);  
        }
    }// for dropped

    print("Added Elements "+newlyAdded);
    print("Deleted Elements "+dropped); 
}

fn100();

From the above code you can see I have written two for loops i.e for added and for dropped.

Is there any better logic to do this ?

Please suggest me.

Rahul Shivsharan
  • 2,481
  • 7
  • 40
  • 59
  • I don't really think this question is a duplicate. He's not asking how to do a diff, the code for that is in the question. He's asking how to do two diffs more efficiently. – Seph Reed Mar 13 '16 at 08:11

6 Answers6

2

You can define a funtion like this..

Array.prototype.diff = function(a) {
    return this.filter(function(i) {return a.indexOf(i) < 0;});
};

And then to simply retrieve the added and deleted element you can do :

var added = edited.diff(original);
//added = [120,140,70]

var deleted = original.diff(edited);
//deleted = [10,100];

Check this link for more details..

Community
  • 1
  • 1
pritesh
  • 2,162
  • 18
  • 24
  • For those wondering how filter works: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter?redirectlocale=en-US&redirectslug=JavaScript%2FReference%2FGlobal_Objects%2FArray%2Ffilter – Seph Reed Mar 13 '16 at 07:38
0

You can easily do this by using the following code-

var edited = [80,120,140,70,160,30];
var original = [10,80,30,100,160];

var added = $.grep(edited, function(el) { return $.inArray(el, original) == -1; }); // returns added= [120,140,70]
var deleted = $.grep(original, function(el) { return $.inArray(el, edited) == -1; }); //returns deleted = [10,100]

Another alternative using filter() function

deleted = original.filter( function( el ) {
      return edited.indexOf( el ) < 0;
}); //returns [10,100]
added = edited.filter( function(el) {
      return original.indexOf(el) < 0;
}); //returns[120,140,70]

Hope this helps!

Shekhar Chikara
  • 3,786
  • 2
  • 29
  • 52
0

Try this jquery function

var original = [10, 80, 30, 100, 160];
var edited = [80, 120, 140, 70, 160, 30];

 var addedElements   = $(edited).not(original).get();
 var droppedElements = $(original).not(edited).get();
Akshay G
  • 2,070
  • 1
  • 15
  • 33
0

If you use underscore.js

its a one liner.

var original = [10,80,30,100,160];
var edited = [80,120,140,70,160,30];

var dif = _.difference(original, edited);
var dif1 = _.difference( edited, original);

console.log(dif)
console.log(dif1)

working code here

Cyril Cherian
  • 32,177
  • 7
  • 46
  • 55
0

if i understand your question well, underscore.js has an easy solution for you:

http://underscorejs.org/#without

this func. basicly compares arr1 to arr2 returning all of array2 what is different from arr1. if you need the whole difference, you can use the func 2 times:

compare arr1 to arr2 and store it compare arr2 to arr1 and store it

merge the results

iModi
  • 101
  • 12
0

There are libraries that do this. I don't know what logic they use but there is better logic available that I can see.

Rather than running four for loops (2x2), run two: (Uncompiled, untested, semi-sudo code below)

var orig = [1, 2, 3]
var changed = [1, 2, 4]
var checklist = changed.slice(0);
var removed = [];
var similar = [];

//go through original
for(i_or in orig) {
    var target = orig[i_or];

    var found = false;
    //check for item in checklist
    for(i_ch in checklist) {

       //if it exists, remove it from checklist.  It's similar
       //different from indexOf because it removes all duplicates
       if(checklist[i_ch] == target) {
          checklist = checklist.splice(i_ch, 1)
          i_ch--;  //don't forget about it!
          similar.push(target) //if you want similar
          found = true;  
       }
    }

    //if not found, it's been removed
    if(found == false)
       removed.push(target)
}

console.log("deleted ", removed);
console.log("new ", checklist);  //anything left in checklist is new 
console.log("same ", similar);

This is probably not the best possible way to do this, but it is better logic.

Also, javascript arrays are often linked lists, so certain functions (like splice) aren't as awful as they may seem.

Community
  • 1
  • 1
Seph Reed
  • 8,797
  • 11
  • 60
  • 125