0

I'm trying to check through a loop each element of this 2D array:

matrix = [[0, 1, 1, 2], 
          [0, 5, 0, 0], 
          [2, 0, 3, 3]]

But, when I do: return matrix.length I get a 3. How is it possible for me to loop through it to check each value?

Also, take in consideration I may get an array like this too:

[[2], 
 [5], 
 [10]]
user120242
  • 14,918
  • 3
  • 38
  • 52
Daniel Logvin
  • 502
  • 7
  • 26
  • Please mention what is expected – Vaibhav May 28 '20 at 07:40
  • Why not use two nested loops? – VLAZ May 28 '20 at 07:41
  • @VLAZ OP probably should have put more effort into it, but, a short and elegant solution like Ifaruki's .flat().length is worth mention – user120242 May 28 '20 at 07:48
  • @user120242 I'm not convinced OP needs the total number of items in the array. The question mentions checking each element. It also mentions the length but I don't believe that's what is asked for. The question is unclear either way - I did my best guess as to what OP wants. – VLAZ May 28 '20 at 07:52
  • @VLAZ has a point. I'm trying to sum the elements of the array. Even though, I don't want for stackoverflow to answer the riddle for me. – Daniel Logvin May 28 '20 at 07:55
  • @Daniel Logvin Just use a combination of mine and Ifaruki's answers for the sum. It's a simple one pass comprehension. I'd have added a vote to close this for lack of effort, to add on to the close for dupe of "For loop in ...", if it weren't that there are short solutions worth mentioning and almost all of the duplicate answers are overly verbose and inelegant (or more accurately probably a bit outdated due to lack of ES6). – user120242 May 28 '20 at 08:01

2 Answers2

4

Use .flat() and simply add after it .length

let matrix = [[0, 1, 1, 2], 
              [0, 5, 0, 0], 
              [2, 0, 3, 3]]
          
let result = matrix.flat().length;
console.log(result);
 
//sum

let sum = matrix.flat().reduce((a,b) => a + b);
console.log(sum);

You might need an Polyfill if you want to run this code on an older browser. Check out the support:

https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Array/flat

https://caniuse.com/#search=flat

bill.gates
  • 14,145
  • 3
  • 19
  • 47
  • @DanielLogvin i readed you need the sum, i have added an `.reduce()` to get the sum of it – bill.gates May 28 '20 at 07:59
  • I'd mention that `Array#flat` is a relatively new feature (standardized in ES2019), so, if the code is used on the client-side and isn't transpiled, the OP may need a polyfill... – FZs May 28 '20 at 08:25
  • 1
    @FZs yes, works on every modern webbrowser expect IE ofcorse https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Array/flat but yea i will mention it – bill.gates May 28 '20 at 08:26
1

reduce, add each length and accumulate sum

matrix = [[0, 1, 1, 2], 
          [0, 5, 0, 0], 
          [2, 0, 3, 3]]
          
console.log(
matrix.reduce((sum,x)=>sum+x.length,0)
)
user120242
  • 14,918
  • 3
  • 38
  • 52