Here is the structure:
const books={
 "1":[["foo", 5],["bar", 8],["noo", 9]],
 "2":[["bmk", 7]],
 "7":[["jol", 1],["lon", 2]]}


Expected Result: 6

What is the modern way?

2 Answers 2

Object.values ​​(books) .flat().length

or

[].concat(...Object.values ​​(books)).length

or

Object.values ​​(books) .reduce((acc, n)=>acc + n.length, 0)
Or like this:
const books={
 "1":[["foo", 5],["bar", 8],["noo", 9]],
 "2":[["bmk", 7]],
 "7":[["jol", 1],["lon", 2]]
}
let count;
for(const key in books) {
  if(typeof books[key] =='object') {
  	count +=books[key] .length
    console.log(count)
  }
}
  • Your code is NaN
    To fix this you can write
    let count=0;
    But this, of course, is not quite fashionable and modern :)
    – Chinese Paintball May 22 '19 at 09:52
  • [[pcdesign]], exactly.From the phone is not very convenient) – Topsy-Turvy Helicopter May 22 '19 at 10:07