javascript - jQuery splice inArray with multiple key objects -


javascript newbie here --

i have following array:

var group = ({          one: value1,         two: value2,         three: value3     }); 

i want check if array "group" part of "groupsarray" , add if doesn't or remove if does.

var grouplocate = $.inarray(group, groupsarray); if(grouplocate ==-1){         groupsarray.push(group);         } else {         groupsarray.splice($.inarray(group, groupsarray),1);         } 

this method works single value arrays. unfortunately, can't work in case 3 keys , values grouplocate returns -1.

what doing wrong?

thanks.

first helps understand why $.inarray() didn't work. let's try simpler case. paste in javascript console in browser on page jquery loaded (such page we're on) , run it:

var object = { a: 1 }; var array = [ { a: 1 } ]; console.log( '$.inarray: ', $.inarray( object, array ) ); 

(note terminology: group variable object, not array.)

now looks object in array, right? why print -1 then? try this:

console.log( object ); console.log( array[0] ); 

they same. how about:

console.log( '== or === works? ', object == array[0], object === array[0] ); 

or simpler:

console.log( 'does {a:1} == {a:1}? ', {a:1} == {a:1} ); console.log( 'what {} == {}? ', {} == {} ); 

those print false!

this because 2 objects happen have same content still 2 separate objects, , when use == or === compare 2 objects, testing whether both references 1 , same object. 2 different objects never compare equal, if contain same content.

$.inarray() works using === operator compare 2 objects - won't find object in array unless the same object, not object identical content.

knowing this, suggest possible ways approach problem? there several ways write own code search array object, or may find helpful use library such underscore.js has many useful methods arrays , objects.

for example, use _.findwhere( groupsarray, group ) find first match - caveat compares properties in group object. example, if group {a:1}, match object in groupsarray array {a:1,b:2}.

if need exact match, combine underscore's _.find() , _.isequal() methods:

var index = _.find( groupsarray, function( element ) {     return  _.isequal( element, group ); }); 

now 1 last thing watch out for. code pushes group object onto groupsarray array - know pushes actual group object itself. doesn't make copy of in array, it's reference same object. (ironically, means original code find group in array work in case you'd pushed same group object onto array yourself.)

if want make sure elements in groupsarray each own independent object, , not reference object floating around in code, can use underscore method shallow copy:

groupsarray.push( _.clone(group) ); 

if group has nested objects, though, won't copy them. (i don't see deep copy function in underscore, although write 1 if need it.)


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -