Skip to content Skip to sidebar Skip to footer

Removing Entry(s) From Json Array Based On Key/value

This is what my object looks like (I don't have a lot of control over how it's formatted): [{ 'Country': 'Spain', 'info info1': 0.329235716, 'info info2': 0.447683684,

Solution 1:

Underscore.js is the library to use for all things data. Functions that will be useful for your data are; each, reject and groupBy.

Solution 2:

Damn, forget about jQuery! This is a pure javascript task.

  • To loop through an Array, use for (var i=0; i<array.length; i++), to loop over an Object use for (var i in object)
  • To delete an array entry, use splice(). To delete an object attribute, use delete object[key];.
  • To filter an Array, in modern javascript versions you can use filter() with a callback function for every entry; it creates a new Array. There is no such (native) method for objects.

You may find some libraries with helpful utility functions, like underscore. But to learn JS, it may be better to do it with native methods first.

Solution 3:

$(output_json).each(function(i){
    if (this["Country"] == "USA") output_json.splice(i,1);
    elseif (this["Country"] == "Spain") this["Country"] = "Brazil";
    else {
        var country = this;
        $(this).each(function(i){
            if (this["info info2"] == 1.302673893) {
                country.splice(i,1);
            } 
        });
    }
})

That's the skeleton of the code - obviously you probably have a better way of detecting renames, removals, etc. But the use of the splice method and the square brackets make this safer than using delete, and more efficient than maintaining multiple arrays, etc.

Solution 4:

If you don't want to write the code directly to do the operations you want, you can use a library like underscore.js which packages up a lot of handy functions for manipulating collections. So after you've parsed your JSON, you could, for example, use reject() to filter out a specific object in your collection.

Solution 5:

I believe the following code does roughly what you're asking for. Check out the working fiddle: http://jsfiddle.net/pUhDR/3/ By the way, I agree with @Gazler, you could use the underscore library, it's more high level and easier to read than this:

var countriesString = JSON.stringify([{
    "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 countries = JSON.parse(countriesString);

// create new arrayvar newCountriesArray = newArray();
// exclude some countriesvar excludeCountries = newObject();
excludeCountries['Spain'] = true;
// rename some countriesvar renameCountries = newObject();
renameCountries['USA'] = 'United States of America';

for (index in countries) {
    if (countries[index].Countryin renameCountries) {
        countries[index].Country = renameCountries[countries[index].Country];
    }
    if (!(countries[index].Countryin excludeCountries)) {
        newCountriesArray.push(countries[index]);
    }
}

alert(JSON.stringify(newCountriesArray));

Post a Comment for "Removing Entry(s) From Json Array Based On Key/value"