javascript - Get the total countlength of arrays within objects ES6 - Stack Overflow

I'm trying to get the total length count of each array within their respective objects...campus :

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 0
Add a ment  | 

3 Answers 3

Reset to default 3

You 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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信