0

I have an array which I populate as following. How do I remove an element from the array?

myArray.push( { "name": "one", "value": "1" } );
myArray.push( { "name": "two", "value": "2" } );

Update: I would have to remove specific element from the array, say { "name": "one", "value": "1" } element from the array.

0

3 Answers 3

2

You can use jQuery.grep to filter out the one you want to remove

var myArray = [];
myArray.push( { "name": "one", "value": "1" } );
myArray.push( { "name": "two", "value": "2" } );
myArray = jQuery.grep(myArray, function(value) {
  return value.name != "one"; // return only if name isn't "one"
});
console.log(myArray); // now myArray doesn't contain  { "name": "one", "value": "1" }  in the array

FIDDLE

Sign up to request clarification or add additional context in comments.

Comments

1

You could use built-in functions (links : splice, pop, shift) :

var a = [1, 2, 3, 4, 5, 6];
a.splice(1, 1); // [2]     a -> [1, 3, 4, 5, 6]
a.splice(1, 2); // [3, 4]  a -> [1, 5, 6]
a.pop();        // 6       a -> [1, 5]
a.shift();      // 1       a -> [5]

Or create your own functions to check some criteria :

// returns the removed element

function removeFirst(array, fn) {
    var i = 0;
    while (i < array.length) {
        if (fn(array[i]) !== true) i++;
        else return array.splice(i, 1)[0];
    }
}

// returns an array containing the removed elements

function removeAll(array, fn) {
    var i = 0, removed = [];
    while (i < array.length) {
        if (fn(array[i]) !== true) i++;
        else removed.push(array.splice(i, 1)[0]);
    }
    return removed;
}

Usage examples :

var array = [
    { name: 'one', value: 11 },
    { name: 'one', value: 1 },
    { name: 'two', value: 2 },
    { name: 'three', value: 3 },
    { name: 'four', value: 4 }
];

// removed -> { name: 'one', value: 1 }

var removed = removeFirst(array, function (item) {
    return item.name === 'one' && item.value === 1;
});

// removed -> [{ name: 'two', value: 2 }, { name: 'three', value: 3 }]

var removed = removeAll(array, function (item) {
    return item.name.charAt(0) === 't';
});

// array -> [{ name: 'one', value: 11 }, { name: 'four', value: 4 }]

Comments

-1
var myArray = [];
myArray.push( { "name": "one", "value": "1" } );
myArray.push( { "name": "two", "value": "2" } );


//console.log(myArray.toSource()); //FF displays output, google chrome gives error

alert(JSON.stringify(myArray));
//output   [{name:"one", value:"1"}, {name:"two", value:"2"}]



var index = myArray[ 'one' ];
myArray.splice(index, 1);

//console.log(myArray.toSource());   //FF displays output, google chrome gives error

alert(JSON.stringify(myArray));
//output  [{name:"two", value:"2"}]

5 Comments

@wared I tested and posted. To cross-verify, just now tested again and I get the result as I posted : [{name:"one", value:"1"}, {name:"two", value:"2"}] then [{name:"two", value:"2"}] I see the output on my firebug console.
@wared Yes, you are correct. Google Chrome shows error as you mentioned. I tested in Firefox (firebug). I've added the code to display the output. Problem was only in displaying the output. Thanks.
Oh sorry! Now I understand, this is not a standard feature : developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…. Sorry again :/
You will hate me... console.log(index) prints undefined. I guess undefined is treated as 0 by splice. So, your suggestion will always remove the first element.
Making a single check of the overall code is not enough if you're not able to predict the result of each constituent parts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.