1

How i can get the difference of two arrays ?

I think need compare the index and delete the position?

The array cann have letter (example Months)

// Round 1
var array1 = [];
var array2 = [10, 2, 3, 5];
//diff: 10, 2 ,3 ,5

// Round 2
var array1 = [10, 2, 3, 5];
var array2 = [10, 2, 3, 5, 2, 5, 11, 9];
//diff: 2, 5, 11, 9

// Round 3
var array1 = [10, 2, 3, 5, 2, 5, 11, 9];
var array2 = [10, 2, 3, 5, 2, 5, 11, 9, 1, 5, 12, 10];
//diff: 1, 5, 12, 10
Constantin Groß
  • 10,719
  • 4
  • 24
  • 50
victorelec14
  • 178
  • 12
  • 2
    What is that you have tried? – void Aug 16 '18 at 13:53
  • 1
    How much time did you put into research, because 1 search on google and you get https://stackoverflow.com/questions/10927722/compare-2-arrays-which-returns-difference – Carsten Løvbo Andersen Aug 16 '18 at 13:55
  • possible duplicate of https://stackoverflow.com/questions/1187518/how-to-get-the-difference-between-two-arrays-in-javascript – Nisarg Aug 16 '18 at 13:55
  • but i need respect the index, the old data are not update, always the same. – victorelec14 Aug 16 '18 at 13:56
  • 1
    Possible duplicate of [How to get the difference between two arrays in Javascript?](https://stackoverflow.com/questions/1187518/how-to-get-the-difference-between-two-arrays-in-javascript) – Scath Aug 16 '18 at 13:57
  • 1
    Possible duplicate of [Compare 2 arrays which returns difference](https://stackoverflow.com/questions/10927722/compare-2-arrays-which-returns-difference) – Ahmed Sunny Aug 16 '18 at 13:58

1 Answers1

0
var array1 = [10, 2, 3, 5, 2, 5, 11, 9];
var array2 = [10, 2, 3, 5, 2, 5, 11, 9, 1, 5, 12, 10];

var diffArray= array2.splice(array1.length,array2.length-array1.length);
console.log(diffArray)

assuming array1 is subArray of array2

Atul
  • 420
  • 2
  • 10
  • Thx, thats run perfect !! – victorelec14 Aug 16 '18 at 14:00
  • @VictorMoscosoLembcke, remember it is always good to [upvote](//stackoverflow.com/privileges/vote-up) this question for future readers if this was useful to you, and consider [accepting](http://stackoverflow.com/help/someone-answers) it if you think it is the best one for your question. – lealceldeiro Aug 17 '18 at 13:36