1

I have an array as follows in nodejs

 var dataary=[];
    dataary=[ [ 'reg_no', 'slno', 'name', 'email', 'rollno' ],
    [ 'int', 'int', 'varchar', 'varchar', 'int' ],
    [ '100', '11', '255', '255', '100' ] ]

I need the count of array elements.As i do dataary.length it will return 3. But i need the count as 5 (count of elements inside array).How can i get the count of elements. ?

Anuja vinod
  • 99
  • 3
  • 11
  • Welcome to Stack Overflow! Please take the [tour] and read through the [help], in particular [*How do I ask a good question?*](/help/how-to-ask) Do your research, [search](/help/searching) for related topics on SO, and give it a go. ***If*** you get stuck and can't get unstuck after doing more research and searching, post a [mcve] of your attempt and say specifically where you're stuck. People will be glad to help. Good luck! – T.J. Crowder Dec 19 '17 at 11:13
  • 1
    use `dataary[0].length`. – Vipin Kumar Dec 19 '17 at 11:14
  • 1
    Note: It just so happens that the length is 5 for all of the arrays in `dataary`, but it could just as easily be that they each had different lengths. – T.J. Crowder Dec 19 '17 at 11:14
  • `But i need the count of elements inside each array` - loop through the main array and extract the length of each sub array? - https://stackoverflow.com/questions/12502843/finding-length-of-all-arrays-multidimensional-array-java – Nope Dec 19 '17 at 11:17
  • 1
    `But i need the count as 5 ` You need to get answer as 5 or 15? – Shalitha Suranga Dec 19 '17 at 11:17

3 Answers3

3

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;
});
David Vicente
  • 3,091
  • 1
  • 17
  • 27
0

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);
    });
manikant gautam
  • 3,521
  • 1
  • 17
  • 27
0

I would do it that way...

dataary.reduce((count, innerArray) => count + innerArray.length, 0);
oae
  • 1,513
  • 1
  • 17
  • 23