2

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?

0

3 Answers 3

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);
Sign up to request clarification or add additional context in comments.

2 Comments

mapping causes a warning thrown in console stating that i need unique ids for each mapped item. this doesn't necessarily need ids. But your solution works nontheless :)
What warning are you seeing? Array.prototype.map shouldn't be able to throw a warning and doesn't require unique IDs, or IDs of any kind.
2

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);

Comments

2

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));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.