0

I've a nested object with a country code. I need to fetch the corresponding currency property using lodash based on the condition.

I'm a newbie to lodash and not sure which method can I use here.

For e.g., When I search for corresponding currencies for the countryCode 'AU', it should return 'AUD'.

Currently, I get the entire object.

How do I get this done?

var myJSON = {
  "countryCode": {
  "Australia": "AU",
  "United States": "US",
  "Britain": "GB",
  "Japan": "JP",
  "India": "IND",
  "France": "FR",
  "Russia": "RS"
},
"countries": {
  "AE": {
    "currencies": {
    "AED": {
      "isDefault": true
    }
    }
  },
  "AL": {
    "currencies": {
    "ALL": {
      "isDefault": true
    }
    }
  },
  "AU": {
    "currencies": {
    "AUD": {
      "isDefault": true
    }
    }
  },
  "US": {
    "currencies": {
    "USD": {
      "isDefault": true
    }
    }
  },
  "GB": {
    "currencies": {
    "EUR": {
      "isDefault": true
    }
    }
  },
  "FR": {
    "currencies": {
    "EUR": {
      "isDefault": true
    }
    }
  },
  "JP": {
    "currencies": {
    "JPY": {
      "isDefault": true
    }
    }
  },
  "RS": {
    "currencies": {
    "RSD": {
      "isDefault": false
    }
    }
  },
  "ZA": {
    "currencies": {
    "ZAR": {
      "isDefault": true
    }
    }
  }
  }
};

function getData() {
 var countryCode = ['AU', 'ZA', 'JP'];
 console.log(_.pick(myJSON.countries, countryCode));
 console.log(_.values(_.pick(myJSON.countries, countryCode)));	
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.core.js"></script>
<button onclick="getData()">Get Data</button>

Current Output: {AU: {…}, ZA: {…}, JP: {…}}

Desired Output:

['AUD', 'ZAR', 'JP']

Here's the code:

3 Answers 3

2

The lodash way to do it looks like that:

function getData() {
 var countryCode = ['AU', 'ZA', 'JP'];
 console.log(
   _(myJSON.countries)
    .pick(countryCode)
    .values()
    .map(function(value) { return _.keys(value.currencies)[0]; })
    .value()
 );	
}

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, but that is giving me countryCodes. I need the currencies.
Ah, true. Gonna change in in a sec
I misunderstood it, because of the "Desired Output: ['AUD', 'ZAR', 'JP']"
Yes, that's how it is supposed to display as per the desired output. But, what I get with your solution is exactly the same as CountryCode.
1

When I search for corresponding currencies for the countryCode 'AU', it should return 'AUD'.

Try this vanila js solution as well, use map

var countryCode = ['AU', 'ZA', 'JP'];
var output = countryCode.map( s => Object.keys( myJSON.countries[ s ].currencies[ 0 ] ) );

5 Comments

Unfortunately, this code won't work in old browsers.
@Alex due to arrow functions?
Yep, Object.keys and array.map won't work in olders IEs as well :(
How old are we talking about? map and Object.keys are quite old!
Like in IE8 :D. Actually I like your answer, it should work in 95% of cases, I gave another answer, just only because lodash was already mentioned (and it looks prettier MHO).
0

Pick will do what its asked to do.

Instead do this :

countryCode.map((ct) => Object.keys(myJSON.countries[ct].currencies)[0]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.