javascript delete item from array -
i have js object
var myclass = class.extend({ _innerarr: [], add: function( id, url ){ this._innerarr.push([id, url]); }, delete: function( id ){ $( this._innerarr ).each(function( index ){ if ( $( )[0]==id ){ this._innerarr.splice( index, 1); // doesn't work!!! uncaught typeerror: cannot call method 'splice' of undefined } }); } }); but, if code change on:
var globalarr; var myclass = class.extend({ _innerarr: [], add: function( id, url ){ this._innerarr.push([id, url]); }, delete: function( id ){ globalarr = this._innerarr; $( this._innerarr ).each(function( index ){ if ( $( )[0]==id ){ globalarr.splice( index, 1); // work!!! } }); } }); why this._innerarr not work? don't want using adding variable in project. thinks, other way is...
when using jquery's .each() method, function gets called this current value of array. you're using when doing $( )[0]. ($( )[0] might unnecessary btw.)
you don't need create global variable this, might set scoped variable in delete function.
alternately, can use for loop instead of jquery's each(). that's little faster.
Comments
Post a Comment