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,
"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
}]
There will be various countries I need to keep the data for, but change the country name. There will also be countries I need to omit altogether.
This is one of those times that I've bitten off more than I can chew all at once, and the work I've done so far is just tire spinning.
$.each(output_json, function() {
$.each(this, function(k, v) {
console.log(k);
});
});
I was attempting to loop through it in this manner, but I'm just getting key/value pairs and I'm not sure how to remove an entire entry based on what I find inside.
Does anyone know of a function/library/jQuery plugin that will quickly allow me to manipulate the object in the manner I've described?
Any help would be GREATLY appreciated.
$(output_json).each(function(i){
if (this["Country"] == "USA") output_json.splice(i,1);
else if (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.
Underscore.js is the library to use for all things data. Functions that will be useful for your data are; each, reject and groupBy.
Damn, forget about jQuery! This is a pure javascript task.
for (var i=0; i<array.length; i++)
, to loop over an Object use for (var i in object)
splice()
. To delete an object attribute, use delete object[key];
.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.
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.
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 array
var newCountriesArray = new Array();
// exclude some countries
var excludeCountries = new Object();
excludeCountries['Spain'] = true;
// rename some countries
var renameCountries = new Object();
renameCountries['USA'] = 'United States of America';
for (index in countries) {
if (countries[index].Country in renameCountries) {
countries[index].Country = renameCountries[countries[index].Country];
}
if (!(countries[index].Country in excludeCountries)) {
newCountriesArray.push(countries[index]);
}
}
alert(JSON.stringify(newCountriesArray));
You can use delete
operator on the array and delete the element.
Code:
var toRemove = [];
$.each(output_json, function(i) {
$.each(this, function(k, v) {
if (v == 'Chile') toRemove.push(i);
});
});
//remove all marked
for (var i = 0; i < toRemove.length; i++) {
delete output_json[toRemove[i]];
}
k
key value is simply the numeric index of the particular sub-object you're currently at in the .each() loop. When you find the element you want nuked, delete it, then return false from the closure to abort the .each loop - Marc B 2012-04-04 21:26