Reputation: 167
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.
Upvotes: 1
Views: 2183
Reputation: 9370
I know I'm late to the party but for completeness 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);
Upvotes: 0
Reputation: 73998
You can consider the following code:
Object.keys()
to "loop" on your object properties.length
of each array
to know how many items are in the array.count
accordingly.Please note, your MyObject
was malformed because:
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);
Upvotes: 0
Reputation: 3120
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)
Upvotes: 1
Reputation: 159
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 recommended 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);
Upvotes: 0
Reputation: 104795
You can use reduce
for everything!
var totalLength = Object.keys(MyObject).reduce(function(total, key) {
return total += MyObject[key].length;
}, 0);
Upvotes: 2
Reputation: 128856
Use 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);
Upvotes: 5