underscore.js - filter using underdescore.js -
var data 1 = [{"year": 2000, "country": "Madagascar", "country_id" : "Economy Statistics"}, "Group": "Economic Statistics"}, "Economic Statistics"}, "Economic Statistics"}, "Year": 2005, "Country": "Madagascar", "Country_ID": 847, "Year": 2000, "Country": "Madagascar", "Country_ID": 847, "Group": "Economic Statistics"}, {"Year": 2005, "Country": "Libya", "Country_ID": 846 "Group": "" Demographics and Social Statistics "}, {" Year ": 2008," Country ":" Libya "," Country_ID ": 846," Group ":" People "Demographic and Social Statistics"}, {"Year": 2005, "Country": "Libya", "Country_ID": 846, "Group": "Demographic and Social Statistics"}] This is my JSON data, I want it filter based on country and year (unique) underscore.js
Depending on some key, you can use the (or _. Unique ) method if you want to select unique elements. An element for Takes the callback to change the key to the key, the array with the same content looks different, so you will need to refresh your key; JSON.stringify can be used for this (or you can use with + - results): data1 = _. Uniq (data1, function (x) {return JSON.stringify ([x.year, x.country_id])}); Demo:
If you want to keep the value only for certain code for year and country_id It is also easy: data1 = _ Filter (data1, function (x) {return x.year === target_year and x.country_id === target_country_id});
Comments
Post a Comment