i wrote jquery custom fn extension script to remove the given element in an array,
but it works unexpected way so i want to figure out how to make it do reference manipulation without using return reference way.
$.fn.removeElement=function(e){
var l=this.length;
while(l--){
if(this[l]==e){
console.log(this);
this.splice(l,1);
console.log(this);
break;
} } }
var x=['a','b','c','d','e'];
console.log(x);
$(x).removeElement('c');
console.log(x);
result is like the below,
> ["a", "b", "c", "d", "e"]
> ["a", "b", "d", "e"]
> ["a", "b", "d", "e"]
> ["a", "b", "c", "d", "e"] //???
i expect [“a”, “b”, “d”, “e”] in the forth line.
p.s. i don’t want ‘return stuff’..
For one, this line of code:
is like this in your code example:
which is
which will never match.
I would also ask why you’re trying to wrap a normal array in a jQuery object. It would be better to just use a method that operates on an array.
As a global function, you could do it like this:
As a global method on the jQuery object, you could do it like this:
As a method on the Array object, you could do it like this:
Working example of the prototype method here: http://jsfiddle.net/jfriend00/tEXWH/