Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

247
Views
deconstruct JSON object in JS

I'm trying to deconstruct this object to get "Swiss franc", assuming i don't know the key CHF.

currencies = {CHF: {name: "Swiss franc", symbol: "Fr."}}

My attempts:

Object.values(Object.values(currencies)[0] 
// -> Swiss franc,Fr.

Object.values(Object.values(currencies)[0].name
// -> S,w,i,s,s, ,f,r,a,n,c
  // why does it split the string?? not retrieve the value based on key name?? 
    // i know how to join the strings back, but i'm confused why is this result?)
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

just a little modification to your code:

const currencies = {CHF: {name: "Swiss franc", symbol: "Fr."}}

console.log(Object.values(Object.values(currencies)[0])[0])

// OR Simply 

console.log(Object.values(currencies)[0].name)

about 4 years ago · Juan Pablo Isaza Report

0

Object.entries Implementation Reference

Explanation

The Object.entries() method returns an array of a given object's own enumerable string-keyed property [key, value] pairs. This is the same as iterating with a for...in loop, except that a for...in loop enumerates properties in the prototype chain as well.

Working Example

const currencies = { CHF: { name: "Swiss franc", symbol: "Fr.", }}
console.log(Object.entries(currencies)[0][1].name);

Code Explanation

Object.entries(currencies)

The above statement return an array as below

[["CHF", {symbol: 'Fr.', name: 'Swiss franc'}]]

An array of combination of key value pairs in the object.

We are interested in the node zero of the array and index one of the first node of array.

So it will be

Object.entries(currencies)[0][1].name

Object.values implementation. Reference.

Explanation

The Object.values() method returns an array of a given object's own enumerable property values, in the same order as that provided by a for...in loop. (The only difference is that a for...in loop enumerates properties in the prototype chain as well.)

Working Example

const currencies = { CHF: { name: "Swiss franc", symbol: "Fr.", }}
console.log(Object.values(currencies)[0].name);

Code Explanation

Object.values(currencies)

Tie above expression will return the array of values. Since we have only one key, the output of above statement will be

[{ symbol: 'Fr.', name: 'Swiss franc' }]

So the requird node will be

Object.values(currencies)[0].name
about 4 years ago · Juan Pablo Isaza Report

0

currencies = {CHF: {name: "Swiss franc", symbol: "Fr."}}

This will do it, Object.values(currencies)[0].name

Breakdown :-

Object.values(currencies) gives [{name: "Swiss franc", symbol: "Fr."}]

Object.values(currencies)[0] gives {name: "Swiss franc", symbol: "Fr."}

Object.values(currencies)[0].name gives Swiss franc.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!