Home > Net >  get specific element in array of objects javascript
get specific element in array of objects javascript

Time:03-07

Given that I have

const countArray = [{count: 97}, {count: 100}, {count: 101}, {count: 102}, {count: 99}, {count: 101}];

What could I do to get 97 for example?

here's what Ive tried

console.log(Object.values(countArray[0]));

this outputs Array [97], but im trying to get just 97.

any help would be appreciated

Note: Ive already looked at similar Stack Overflow posts

CodePudding user response:

Object.values() always returns array. Reference - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Object/values

so you can try this console.log(Object.values(countArray[0])[0]);

and if the key will be same for all the objects then you can try console.log(countArray[0]['count'] );

  • Related