I've created a simple filtering code that filters out 0s and nulls. Perhaps not the most elegant, but it works:
var data = [{"location": 30, "year": 2019},
{"location": 60, "year" : 1928},
{"location": 0, "year": 2019},
{"location": 50, "year": 0}];
var filteredData = [];
for (var i = 0; i < data.length; ++i) {
if (data[i].location !== null && data[i].location != 0) {
filteredData.push({
location: data[i].location,
year: data[i].year
})
}
};
console.log(filteredData);
Now I would like to filter out 0s and nulls for other keys (e.g. year), how can I dynamically change key?
My (perhaps naive) thinking was to create a variable indicating the key I want to filter out
Unfortunately that didn't work. Does anyone have a suggestion or help me in the right direction?
var var1 = 'year';
var data = [{"location": 30, "year": 2019},
{"location": 60, "year" : 1928},
{"location": 0, "year": 2019},
{"location": 50, "year": 0}];
var filteredData = [];
for (var i = 0; i < data.length; ++i) {
if (data[i].var1 !== null && data[i].var1 != 0) {
filteredData.push({
location: data[i].location,
year: data[i].year
})
}
};
console.log(filteredData)