I'm trying to get the total length count of each array within their respective objects...
campus : {
"mchale": {
"classes":["ESJ030", "SCI339"], // get the length
"faculty":["Hardy", "Vikrum"] // get the length
},
"lawerence":{
"classes":["ENG001"], // get the length
"faculty":["Speedman", "Lee", "Lazenhower"] // get the length
}
}
This is what I have:
const arrCount = campus.mchale.classes.length + campus.mchale.faculty.length + campus.lawerence.classes.length ...
Is there a better/prettier way to go about this to retrieve the total count of each array present in the objects?
I'm trying to get the total length count of each array within their respective objects...
campus : {
"mchale": {
"classes":["ESJ030", "SCI339"], // get the length
"faculty":["Hardy", "Vikrum"] // get the length
},
"lawerence":{
"classes":["ENG001"], // get the length
"faculty":["Speedman", "Lee", "Lazenhower"] // get the length
}
}
This is what I have:
const arrCount = campus.mchale.classes.length + campus.mchale.faculty.length + campus.lawerence.classes.length ...
Is there a better/prettier way to go about this to retrieve the total count of each array present in the objects?
Share asked May 20, 2016 at 14:47 ModelesqModelesq 5,42220 gold badges63 silver badges89 bronze badges 03 Answers
Reset to default 3You can use Object.keys
with map
and reduce
to collect the arrays, get their length, then sum those values:
const data = {
"mchale": {
"classes":["ESJ030", "SCI339"], // get the length
"faculty":["Hardy", "Vikrum"] // get the length
},
"lawerence":{
"classes":["ENG001"], // get the length
"faculty":["Speedman", "Lee", "Lazenhower"] // get the length
}
};
const count = Object.keys(data).map(campusName => {
const campus = data[campusName];
return Object.keys(campus).map(key => campus[key].length).reduce((p, c) => p + c, 0);
}).reduce((p, c) => p + c, 0);
console.log(count);
You can use Array#reduce
with Object.keys()
as follow.
Object.keys(campus).reduce((a, b) => campus[b].classes.length +
campus[b].faculty.length + a, 0);
var campus = {
"mchale": {
"classes": ["ESJ030", "SCI339"], // get the length
"faculty": ["Hardy", "Vikrum"] // get the length
},
"lawerence": {
"classes": ["ENG001"], // get the length
"faculty": ["Speedman", "Lee", "Lazenhower"] // get the length
}
};
var length = Object.keys(campus).reduce((a, b) => campus[b].classes.length + campus[b].faculty.length + a, 0);
console.log(length);
You can recursively traverse object key's and sum all lengths of array objects:
var campus = {
"mchale": {
"classes":["ESJ030", "SCI339"],
"faculty":["Hardy", "Vikrum"]
},
"lawerence":{
"classes":["ENG001"],
"faculty":["Speedman", "Lee", "Lazenhower"]
}
};
function totalArrayLength(obj) {
return Object.keys(obj).reduce((total, key) => {
if (Array.isArray(obj[key])) {
total += obj[key].length;
} else if (typeof obj[key] === 'object') {
total += totalArrayLength(obj[key]);
}
return total;
}, 0);
}
console.log(totalArrayLength(campus));
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744917097a4600901.html
评论列表(0条)