How To Count Array Elements Inside An Array In Javascript?
I have an array as follows in nodejs var dataary=[]; dataary=[ [ 'reg_no', 'slno', 'name', 'email', 'rollno' ], [ 'int', 'int', 'varchar', 'varchar', 'int' ], [ '100',
Solution 1:
With map you can get all lengths in one array and then you can sum them or do whatever you want to do.
var allLengths = dataary.map(element => {
return element.length;
});
Solution 2:
Iterate through loop and get length of each individual array
.The forEach() method executes a provided function once for each array element.
var dataary=[];
dataary=[ [ 'reg_no', 'slno', 'name', 'email', 'rollno' ],
[ 'int', 'int', 'varchar', 'varchar', 'int' ],
[ '100', '11', '255', '255', '100' ] ,
[ '1', '2', '3' ]]
dataary.forEach(function(element) {
console.log(element.length);
});
Solution 3:
I would do it that way...
dataary.reduce((count, innerArray) => count + innerArray.length, 0);
Post a Comment for "How To Count Array Elements Inside An Array In Javascript?"