-2

My array with element,

var count =[["test1.img","test2.img","test3.img"]];

If I count array element [] single dimensional, The count is working fine But If I use like this [[]], It's not working.

MY Question

How to count the total array elements in a two-dimensional array.

The output should be 3.

Thanks

Ashok Charu
  • 274
  • 2
  • 23

2 Answers2

1

You want the count of the inner array.

  1. The number of elements in an array the simple way is to use Array.length.

  2. In your case, you would use count[0].length.

    var count =[["test1.img","test2.img","test3.img"]];
    var result = count[0].length;
    console.log(result);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script>
Ashok Charu
  • 274
  • 2
  • 23
Mustafa
  • 56
  • 1
  • 2
1

Assuming you have a array of arrays and you want to sum up the length of all the array items. You can try following using Array.reduce

// Code goes here

var arr = [["test1.img","test2.img","test3.img"]];

var count = arr.reduce(function(a, subArray){
  return a + subArray.length;
}, 0);

console.log(count);
Nikhil Aggarwal
  • 28,197
  • 4
  • 43
  • 59