5

I have a two-dimensional array. I'm trying to count the values within the inner array. I'm aware of JS array.length. But doing testData.length yields 5. Which is accurate. However, I want to count the number of items within each sub array.

testData[0].length would yield 6. But how would I dynamically count through each sub array? (as it will change).

var testData = [["column1","test1","test1","tea","party", "water bottle"],
                ["column2","test2","test2","test2 test2"],
                ["column3","test2","test2","test2 test2 "],
                ["column4","test2","test2 test2f asdfsdf"],
                ["column5","test2","test2 test2f asdfsdfasdfasdfasa asda asdfsas"]
]
0

5 Answers 5

12

Use reduce:

ES6:

const count = testData.reduce((currentCount, row) => currentCount + row.length, 0);

ES5:

var count = testData.reduce(function(currentCount, row) { 
    return currentCount + row.length;
}, 0);

Documentation : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce

Sign up to request clarification or add additional context in comments.

Comments

5

Use Array.forEach

s=0;
testData.forEach(function(e,i,a){s += e.length; });

See also: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/forEach

Comments

0
var testData = [
    ["column1","test1","test1","tea","party", "water bottle"],
    ["column2","test2","test2","test2 test2"],
    ["column3","test2","test2","test2 test2 "],
    ["column4","test2","test2 test2f asdfsdf"],
    ["column5","test2","test2 test2f asdfsdfasdfasdfasa asda asdfsas"]
];
for(var i=0;i < testData.length;i++){
  for(var j=0;j<testData[i].length;j++){
    alert(testData[i][j]);
  }
}

1 Comment

Your inner for loop is counting the actual characters within the sub array isn't it? Definitely don't need to go that far. I instead moved console.log(testData[i].length); to the first loop. And it achieves correct results. Thanks.
0

Try adding a new property to the Array object.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <meta http-equiv="content-type" content="text/html; charset=windows-1250">
    <title>New Property</title>
    <script type="text/javascript" language="JavaScript">
      Array.prototype.biLength = function(){
        numItems = 0;
        numRows = this.length;
        for (var i = 0; i < numRows; i++){
          row = testData[i];
          numCols = row.length;
          for (var j = 0; j < numCols; j++){
            numItems++;
          }
        }
        return numItems;
      };
      var testData = [
        ["column1","test1","test1","tea","party", "water bottle"],
        ["column2","test2","test2","test2 test2"],
        ["column3","test2","test2","test2 test2 "],
        ["column4","test2","test2 test2f asdfsdf"],
        ["column5","test2","test2 test2f asdfsdfasdfasdfasa asda asdfsas"]
      ];
      alert(testData.biLength());
    </script>
  </head>
  <body>

  </body>
</html>

Comments

0
var lengths = testData.map(subArray => subArray.length);
console.log(lengths);

1 Comment

Although this code might answer the question, I recommend that you also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.