0

Below code returns the 'not' of two lists arrays :

var original = "test1,test2,test3"

var updated = "test1,test2,test4,test5"

var originalArray  = original.split(',');

var updatedArray  = updated.split(',');

console.log(originalArray.length)
console.log(updatedArray.length)

console.log($(originalArray).not(updatedArray))
console.log($(updatedArray).not(originalArray))

fiddle : http://jsfiddle.net/zhHfG/8/

The value of console.log($(updatedArray).not(originalArray)) is :

["test4", "test5", prevObject: jQuery.fn.jQuery.init[4], context: undefined, selector: ".not(test1,test2,test3)", constructor: function, init: function…]

How can the values "test4", "test5" be accessed ?

I could access them using array accessors [0],[1] but is there a cleaner way since the amount of returned values is variable ?

blue-sky
  • 51,962
  • 152
  • 427
  • 752
  • 1
    Do you want them as an array? `.get()`? – CodingIntrigue Jul 30 '14 at 11:06
  • @RGraham yes, thats what I was looking for. thanks! http://jsfiddle.net/zhHfG/11/ – blue-sky Jul 30 '14 at 11:14
  • Or `.toArray()`? But seriously, don't use jQuery for data manipulation, it's meant for the DOM. – Bergi Jul 30 '14 at 11:15
  • @Bergi but how else to find the difference between two Arrays ? – blue-sky Jul 30 '14 at 11:17
  • By native methods (`filter` + `indexOf`) or by using a [dedicated library](http://underscorejs.org/#difference) – Bergi Jul 30 '14 at 11:21
  • @Bergi but using "not" is a clean abstraction for finding the difference ? – blue-sky Jul 30 '14 at 11:25
  • It's using jQuery, so *no*. While jQuery might have a (mostly) clean-looking syntax, it'y as garbled as IE's DOM underneath. Just compare [clean](http://underscorejs.org/docs/underscore.html#section-56) with [jQuery](http://james.padolsey.com/jquery/#v=git&fn=jQuery.fn.not) source… – Bergi Jul 30 '14 at 11:43
  • 1
    Possible duplicate: http://stackoverflow.com/questions/10927722/jquery-compare-2-arrays-return-difference – mccannf Jul 30 '14 at 14:17

0 Answers0