Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

deleting row from JSON array leaves 'null'

http://jsfiddle.net/J2KuY/

In test2, you can see that instead of removing the node from the array, it is replacing the node with 'null'.

What am I doing wrong, and how can I remove it completely?

Edit: using Splice instead of delete. Updated fiddle here: http://jsfiddle.net/J2KuY/1/

like image 440
S16 Avatar asked Apr 04 '12 22:04

S16


People also ask

How to delete row from array in JavaScript?

Array elements can be deleted using the JavaScript operator delete . Using delete leaves undefined holes in the array. Use pop() or shift() instead.

How to remove specific element from json array in JavaScript?

To remove JSON element, use the delete keyword in JavaScript.

How to remove particular value from array?

Pass the value of the element you wish to remove from your array into the indexOf() method to return the index of the element that matches that value in the array. Then make use of the splice() method to remove the element at the returned index.

How to remove specific element from array JavaScript?

pop() function: This method is used to remove elements from the end of an array. shift() function: This method is used to remove elements from the start of an array. splice() function: This method is used to remove elements from the specific index of an array.


2 Answers

use splice :

http://www.w3schools.com/jsref/jsref_splice.asp

like image 155
galchen Avatar answered Sep 19 '22 03:09

galchen


Here's a sample with chaining

//say you have these arrays
var test1 = [{
    "Country": "Spain",
    "info info1": 0.329235716,
    "info info2": 0.447683684,
    "info info3": 0.447683747},
{
    "Country": "Chile",
    "info info1": 1.302673893,
    "info info2": 1.357820775,
    "info info3": 1.35626442},
{
    "Country": "USA",
    "info info1": 7.78805016,
    "info info2": 26.59681951,
    "info info3": 9.200900779}];

var test2 = [{
    "Country": "Germany",
    "info info1": 0.329235716,
    "info info2": 0.447683684,
    "info info3": 0.447683747},
{
    "Country": "China",
    "info info1": 1.302673893,
    "info info2": 1.357820775,
    "info info3": 1.35626442},
{
    "Country": "France",
    "info info1": 7.78805016,
    "info info2": 26.59681951,
    "info info3": 9.200900779}];


//similar to jQuery, wrap them in an object
function $W(param) {
    var obj = {};

    //set data
    obj.data = param;

    //augment the object with a remove function
    obj.remove = function(key, val) {
        var i = 0;
        //loop through data
        while (this.data[i]) {
            if (this.data[i][key] === val) {
                //if we have that data, splice it
                //splice changes the array length so we don't increment
                this.data.splice(i, 1);
            } else {
                //else move on to the next item
                i++;
            }
        }
        //be sure to return the object so that the chain continues
        return this;
    }

    //return object for operation
    return obj
}

//the following will look strangely similar to jQuery

//remove chile, then USA
var result1 = $W(test1).remove('Country', 'Chile').remove('Country', 'USA');

//remove germany and china
var result2 = $W(test2).remove('Country', 'China').remove('Country', 'Germany');

//should only have spain and france
$('#test2').val(JSON.stringify(result1)+JSON.stringify(result2));​
like image 40
Joseph Avatar answered Sep 22 '22 03:09

Joseph