-1

Hi im new to javascript so don't mind me!

I have this object const obj = {'name': ['jack', 'ric'], 'age': 30}

if i do like this console.log(obj[Object.keys(obj)[0]]); i will get ['jack', 'ric']. How can I get only jack without 'ric' and quote outside?

Carlos
  • 452
  • 2
  • 18

1 Answers1

0

What about this:- You're using obj[Object.keys(obj)[0]]; to get the first value of name object, your code is almost correct you have to place one more [0] to make this working because you're accessing an array property in another object.

Working e.g.

const obj = {'name': ['jack', 'ric'], 'age': 30}
// to print jack in console
console.log(obj[Object.keys(obj)[0]][0]);
// to print ric in console
console.log(obj[Object.keys(obj)[0]][1]);

Hope this will helpful for you, thank you.

Amaan warsi
  • 184
  • 4
  • 18
  • *because you're calling object in another object* no they're not. – Liam Sep 22 '20 at 07:23
  • 1
    Relevant SO question: https://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order – Olian04 Sep 22 '20 at 07:23
  • @Amaanwarsi Thanks you your answer helps me! The first is also correct but the thing is I get the object from json response so i don't know the object key names. Thanks you – Carlos Sep 22 '20 at 07:33