deleting row from JSON array leaves 'null'

Go To StackoverFlow.com

9

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/

2012-04-04 22:55
by S16


3

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));​
2012-04-04 23:18
by Joseph
How is the function reusable with multiple this way - S16 2012-04-04 23:23
the data is wrapped in the object. a function was added to retrieve and remove a specific data. chaining works when the previous function call returns the same object it was operating on. this makes it possible for the next function call to use it, its methods and its data. basically, that's how jQuery also works. it just collects elements into an array, then wrap it in an object and augment it with jQuery functions, that return that object after every operation - Joseph 2012-04-04 23:25
What if I want to use this function on several datasets on the same page - S16 2012-04-04 23:36
you need a function to wrap them in an object. see updated answer and demo - Joseph 2012-04-04 23:43


7

use splice :

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

2012-04-04 22:56
by galchen
w3schools, eh? https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/splic - Jordan 2012-04-04 22:59
w3schools is first in google ; - galchen 2012-04-04 23:03
@Jordan: I don't see any advantage in your comment. When you want to point to a qualified ressource, do it, and the ressource would'nt be MDN, it would be ECMAScript: http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-262.pd - Dr.Molle 2012-04-04 23:09
@galchen,

Follow up question: Why is I can call the function using: csv2 = removeCsvEntry(csv2, 'Country', 'Norway'); OR removeCsvEntry(csv2, 'Country', 'Norway');?

I'm used to php, so I'm curious why the object is affected from within the function. A follow-follow-up to that would be, how would I write that function so I could use it in the following manner: csv2.removeCsvEntry('Country', 'Norway')? Or even chain it?

Thanks - S16 2012-04-04 23:11

@Dr.Molle haven't you heard of.. W3Fools - Joseph 2012-04-04 23:58
@Dr.Molle Yeah, I'm not going to link to a PDF, and I'm not going to link to ECMAScript. I'm going to link to an authoritative resource like a vendor's standard implementation that's easy to read, is indexed, and is of just the function in question. W3Schools is often wrong, out of date, and unreadable - Jordan 2012-04-05 00:30
Ads