Reputation: 605
var items = [{
"name": "James",
"gender": "boy"
}, {
"name": "Sam",
"gender": "boy"
}, {
"name": "Alice",
"gender": "girl"
}];
I want to add count for the gender into a array of object above. like for boy it's 2, and girl it's 1. I've done below part where it return the category.
var genderList = [];
for(var i = 0; i < items.length ; i++){
if(genderList.indexOf(items[i].gender) < 0){
genderList.push(items[i].gender);
}
}
how can I produce the result like this?
[{"name":"boy","count":2},{"girl","count":1}]
Upvotes: 1
Views: 91
Reputation: 150040
You can use the array .reduce()
method to count values - I'd suggest getting output in a structure like {"boy": 2, "girl": 1}
rather than the array of objects you showed, because it's simple and efficient to create, update, and read.
You could then convert it to the format you've shown:
var items = [
{ "name": "James", "gender": "boy"},
{ "name": "Sam", "gender": "boy"},
{ "name": "Alice", "gender": "girl"}
];
var genderCount = items.reduce(function(a, c) {
a[c.gender] = (a[c.gender]||0) + 1;
return a;
}, {});
console.log(JSON.stringify(genderCount));
var asArray = Object.keys(genderCount).map(v => ({ name: v, count: genderCount[v] }));
console.log(JSON.stringify(asArray));
If you're not familiar with .reduce()
, it does the same thing as the loop below, except without needing to create extra working variables in the current scope:
var items = [
{ "name": "James", "gender": "boy"},
{ "name": "Sam", "gender": "boy"},
{ "name": "Alice", "gender": "girl"}
];
var genderCount = {}
for (var i = 0; i < items.length; i++) {
genderCount[items[i].gender] = (genderCount[items[i].gender]||0) + 1;
}
console.log(JSON.stringify(genderCount));
// then as above to get your array of objects output:
var asArray = Object.keys(genderCount).map(v => ({ name: v, count: genderCount[v] }));
console.log(JSON.stringify(asArray));
NOTE: In the above examples I'm using JSON.stringify()
only to make it fit in the console on one line, it's not part of the counting/conversion process.
Upvotes: 1
Reputation: 6005
var items = [
{ "name": "T", "gender": "other"},
{ "name": "James", "gender": "boy"},
{ "name": "Sam", "gender": "boy"},
{ "name": "Alice", "gender": "girl"}
];
taking the previous item and comparing the gender type. If it exists we add 1, if doesn't then we create it and add 1.
var genderTypes = items.reduce(function(prev, cur) {
prev[cur.gender] = (prev[cur.gender]||0) + 1;
return prev;
}, {});
console.log('genderTypes', genderTypes)
next we take the keys from the object and separate them out into gender and count
var separateNameAndCount = Object.keys(genderTypes).map(type => ({ name: type, count: genderTypes[type] }));
console.log('separateNameAndCount', separateNameAndCount)
finally we convert the object to json format
console.log(JSON.stringify(separateNameAndCount));
Upvotes: 1
Reputation: 6994
Try like this.use Array.map
var items = [{
"name": "James",
"gender": "boy"
}, {
"name": "Sam",
"gender": "boy"
}, {
"name": "Alice",
"gender": "girl"
}];
var b=0,g=0;
items.map((item)=>(item.gender=="boy")?b++:g++);
var finalArray = [{"name":"boy","count":b},{"name":"girl","count":g}]
console.log(finalArray);
Upvotes: 0