I have an object like this :
MyObject {
Object1: ["12345", "67890"]
Object2: ["74185"]
Object3: ["29630", "789654"]
}
I need to get the total number of elements. In this exemple , I should get 5. I'm trying to do this in the most efficient way.
I have an object like this :
MyObject {
Object1: ["12345", "67890"]
Object2: ["74185"]
Object3: ["29630", "789654"]
}
I need to get the total number of elements. In this exemple , I should get 5. I'm trying to do this in the most efficient way.
Share Improve this question edited Jul 1, 2016 at 12:39 James Donnelly 129k35 gold badges215 silver badges223 bronze badges asked Jul 1, 2016 at 12:35 CamilleCamille 1678 bronze badges 2- 1 An object does not have a length. If you want to sum the lengths of its array-valued elements, then do so explicitly. Just write a simple loop. – Bergi Commented Jul 1, 2016 at 12:37
- Does all objects have this structure or you want something that can generically count every objects in an object graph? – plalx Commented Jul 1, 2016 at 12:38
6 Answers
Reset to default 5Use a for
loop to loop through the object, checking to ensure the key is an Array before incrementing a length counter:
var count = 0;
for (var key in MyObject)
if (MyObject[key] instanceof Array)
count += MyObject[key].length;
var MyObject = {
Object1: ["12345", "67890"],
Object2: ["74185"],
Object3: ["29630", "789654"]
}
var count = 0;
for (var key in MyObject)
if (MyObject[key] instanceof Array)
count += MyObject[key].length;
console.log(count);
You can use reduce
for everything!
var totalLength = Object.keys(MyObject).reduce(function(total, key) {
return total += MyObject[key].length;
}, 0);
You'll need to iterate over all keys of the map and sum the length of every array.
Functional way to do this:
function add(a, b) { return a + b; }
Object.keys(test).map(function(key) {
return test[key].length
}).reduce(add);
Or if you're on one of the latter Node.js versions:
Object.keys(test).map(key => test[key].length).reduce((a, b) => a + b)
Use for in
loop to iterate all the objects and add the counts by find the length of the array.
While using for in
its is remended to use hasOwnProperty()
to avoid unnecessory properties being iterated due to prototypical inheritance.
var count = 0;
for (var key in MyObject) {
if (MyObject.hasOwnProperty(key)) {
count += MyObject[key].length;
}
}
console.log(count);
You can consider the following code:
- Use
Object.keys()
to "loop" on your object properties. - Use property
length
of eacharray
to know how many items are in the array. - Increment variable
count
accordingly.
Please note, your MyObject
was malformed because:
- There is and assignment operator (=).
- Missing ma (,) after each properties.
- No use of var (I am assuming you do not want MyObject as global object).
var myObject = {
Object1: ["12345", "67890"],
Object2: ["74185"],
Object3: ["29630", "789654"]
};
var count = 0;
Object.keys(myObject).map(function(prop) {
count+= myObject[prop].length;
});
alert(count);
I know I'm late to the party but for pleteness here is my approach using ES2019 introduced method <Array>.flat()
. First you can get all arrays with Object.values()
and then flat()
to merge all arrays together in order to get the total length.
Since the flat()
-method only flattens one layer at a time per default you can pass 2
as an argument (depth).
In terms of code length this is the most efficient way to solve your problem I've seen so far.
const myobject = {
Object1: ["12345", "67890"],
Object2: ["74185"],
Object3: ["29630", "789654"],
};
console.log(Object.values(myobject).flat(2).length);
In case myobject
has nested arrays you can also go with Infinity
. Just to be sure ;)
const myobject = {
Object1: [
"1",
"2",
[
["3", "4"],
["5", "6"],
],
],
Object2: ["7"],
Object3: ["8", "9"],
};
console.log(Object.values(myobject).flat(Infinity).length);