-1

I am working with JavaScript and I am trying to know the length of values of a key. My thought is using forEach but it is not working. What I mean is like knowing the number of values in audi (that will be 2. Ford will return 2. Kia will return 1). My dataset is below and thanks in advance:

const object1 = {
  audi:
   [ { make: 'audi', model: 'r8', year: '2012' },   
     { make: 'audi', model: 'rs5', year: '2013' } ],
  ford:
   [ { make: 'ford', model: 'mustang', year: '2012' },
     { make: 'ford', model: 'fusion', year: '2015' } ],
  kia: [ { make: 'kia', model: 'optima', year: '2012' } ] }
  • 2
    `let audiCount = object1.audi.length, fordCount = object1.ford.length, kiaCount = obj.kia.length;` – StackSlave Aug 14 '20 at 05:22
  • 1
    Does this answer your question? [How do I loop through or enumerate a JavaScript object?](https://stackoverflow.com/questions/684672/how-do-i-loop-through-or-enumerate-a-javascript-object) – Karan Aug 14 '20 at 05:23
  • @Karan It is a different question. I already know how to loop an object by using the length of the object. – Naheem Olaniyan Aug 14 '20 at 05:29
  • @StackSlave. Works well. Thanks. – Naheem Olaniyan Aug 14 '20 at 05:29
  • No hard feeling for you but check all of your answers. You will see they have `loop` over `object` and then simply get `.length` from `values`. That question is not the same but that will provide enough info which can solve your problem. – Karan Aug 14 '20 at 05:35

4 Answers4

2

Use the length property of Arrays.

So in your case, object1.audi.length will give you 2

To get all the lengths, you could do the follwing

let keyLengths = {}
Object.keys(object1).forEach(key => {
  keyLengths[key] = object1[key].length
})
1

Here: use Object.values and then just get the length with value.length

const object1 = {
  audi:
   [ { make: 'audi', model: 'r8', year: '2012' },   
     { make: 'audi', model: 'rs5', year: '2013' } ],
  ford:
   [ { make: 'ford', model: 'mustang', year: '2012' },
     { make: 'ford', model: 'fusion', year: '2015' } ],
  kia: [ { make: 'kia', model: 'optima', year: '2012' } ] }
  
  Object.values(object1).forEach(value => {
     let length = value.length;
     console.log(length);
  })
bill.gates
  • 14,145
  • 3
  • 19
  • 47
1

You could iterate through Object.keys() (doc), and it supports in most browsers (see in Browser compatibility section)

const object1 = {
  audi: [
    { make: 'audi', model: 'r8', year: '2012' },
    { make: 'audi', model: 'rs5', year: '2013' }
  ],
  ford: [
    { make: 'ford', model: 'mustang', year: '2012' },
    { make: 'ford', model: 'fusion', year: '2015' }
  ],
  kia: [{ make: 'kia', model: 'optima', year: '2012' }]
}

Object.keys(object1).forEach(key => {
  console.log(key, object1[key].length)
})
hgb123
  • 13,869
  • 3
  • 20
  • 38
1

I would just use a for in loop, to get them all:

const object1 = {
  audi:
   [ { make: 'audi', model: 'r8', year: '2012' },   
     { make: 'audi', model: 'rs5', year: '2013' } ],
  ford:
   [ { make: 'ford', model: 'mustang', year: '2012' },
     { make: 'ford', model: 'fusion', year: '2015' } ],
  kia: 
   [ { make: 'kia', model: 'optima', year: '2012' } ]
}
for(let k in object1){
  console.log(k+'Count = '+object1[k].length);
}
StackSlave
  • 10,613
  • 2
  • 18
  • 35