I have some data I need to do some computations/manipulation on:
data=[{"sex":"M","age":"0","pop":"310"},
{"sex":"M","age":"5","pop":"306"},
{"sex":"M","age":"10","pop":"313"},
{"sex":"M","age":"15","pop":"332"},
....
{"sex":"M","age":"100","pop":"4"},
{"sex":"W","age":"0","pop":"294"},
{"sex":"W","age":"5","pop":"291"},
{"sex":"W","age":"10","pop":"300"},
{"sex":"W","age":"15","pop":"318"},
....
{"sex":"W","age":"100","pop":"1"}
]
I extract the sub data for male (M) and for female (W) :
var male=data.filter(function(d){
if (data.sex=="M"){return d.pop;}
})
var female=data.filter(function(d){
if (data.sex=="F"){return d.pop;}
})
Now, I would like to compute the population (pop) for both sex i.e. create a new variable named "both" containing with respect of the variable "age" :
both=[{"sex":"MW","age":"0","pop":"604"},
{"sex":"MW","age":"5","pop":"597"},
{"sex":"MW","age":"10","pop":"613"},
{"sex":"MW","age":"15","pop":"650",
....
{"sex":"MW","age":"100","pop":"5"}
]
How do I do that?
-
Your question has nothing to do with JSON. JSON is a textual notation for data exchange. If you're dealing with JavaScript source code, and not dealing with a string, you're not dealing with JSON.T.J. Crowder– T.J. Crowder2016年06月06日 12:51:20 +00:00Commented Jun 6, 2016 at 12:51
3 Answers 3
It sounds like you want to combine the M and W entries into a single entry with a combined pop value, by age. If so, you need to create a map of the entries by age and add the pops together, which you can fairly easily do in ES5 using an object (in ES2015 — aka "ES6" — you'd probably use a Map). Then convert it back into an array when you're done. See comments:
// Your data -- note that I've fixed the pop values, if they're
// meant to be numbers, they shouldn't be in quotes
var data = [
{"sex":"M","age":"0","pop":310},
{"sex":"M","age":"5","pop":306},
{"sex":"M","age":"10","pop":313},
{"sex":"M","age":"15","pop":332},
{"sex":"M","age":"100","pop":4},
{"sex":"W","age":"0","pop":294},
{"sex":"W","age":"5","pop":291},
{"sex":"W","age":"10","pop":300},
{"sex":"W","age":"15","pop":318},
{"sex":"W","age":"100","pop":1}
];
// Create the "map" of entries by age:
var byAge = Object.create(null);
data.forEach(function(entry) {
// Get the entry for this age, if any
var ageEntry = byAge[entry.age];
if (!ageEntry) {
// None, create and add it with an initial pop of 0
ageEntry = byAge[entry.age] = {sex: "MW", age: entry.age, pop: 0};
}
// Add in the pop for this entry
ageEntry.pop += entry.pop;
});
// Convert it back into an array (if you want):
var result = Object.keys(byAge).map(function(key) {
return byAge[key];
});
// Show result:
console.log(result);
1 Comment
var data = [
{"sex":"M","age":"0","pop":310},
{"sex":"M","age":"5","pop":306},
{"sex":"M","age":"10","pop":313},
{"sex":"M","age":"15","pop":332},
{"sex":"M","age":"100","pop":4},
{"sex":"W","age":"0","pop":294},
{"sex":"W","age":"5","pop":291},
{"sex":"W","age":"10","pop":300},
{"sex":"W","age":"15","pop":318},
{"sex":"W","age":"100","pop":1}
];
var ages=[]
for(var i=0;i<data.lenght;i++){
if(!ages[data[i]['age']]){
ages[data[i]['age']].push({"sex": "MW", "age": data[i]['age'], "pop":data[i]['pop'] })
}else{
ages[data[i]['age']].pop +=data[i]['pop']
}
}
Comments
It sounds like the reduce method would work for what you need.
`var population = ARR.reduce(function(a, b) {
return Number(a.pop) + Number(b.pop);
});`
Note that you must cast the string representation of the population to a number, or it will be concatinated.