0

I have an object like this:

 var currencyTypes = { NOK: {value:1.00000, name: "Norske kroner", denomination: "kr" }, EUR: {value:0.10733, name: "Europeiske euro", denomination: "€" }, USD: {value:0.12652, name: "United States dollar", denomination: "$" }, GBP: {value:0.09550, name: "Pound sterling", denomination: "£" }, }; 

And I have found a way to list all the key objects:

var keyVal = []; for(var v in currencyTypes) keyVal.push(v); "There are " + keyVal.length + " different currencies here: " + keyVal 

This lists all the currencyTypes: NOK,EUR, USD, GBP

But how can I print a list with key, value, name, denominations? I tried keyVal.properties but that didn’t work. I've tried to search for a solution here, but haven't found anything. What I want is an output that looks something like this:

NOK, Norske kroner, 1 kr EUR, European euros, 0.10733 € and so on 

6 Answers 6

1

You can access it this way. This might help you.

 var currencyTypes = { NOK: {value:1.00000, name: "Norske kroner", denomination: "kr" }, EUR: {value:0.10733, name: "Europeiske euro", denomination: "€" }, USD: {value:0.12652, name: "United States dollar", denomination: "$" }, GBP: {value:0.09550, name: "Pound sterling", denomination: "£" }, }; for(var type in currencyTypes) { console.log("Currency: " + type); console.log("Value: " + currencyTypes[type].value); console.log("Name: " + currencyTypes[type].name); console.log("Denomination: " + currencyTypes[type].denomination); console.log("\n"); }

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

Comments

1

const currencyTypes = { NOK: {value:1.00000, name: "Norske kroner", denomination: "kr" }, EUR: {value:0.10733, name: "Europeiske euro", denomination: "€" }, USD: {value:0.12652, name: "United States dollar", denomination: "$" }, GBP: {value:0.09550, name: "Pound sterling", denomination: "£" }, }; function compileCurrenciesString(currencies) { let outStr = ''; Object.keys(currencies).forEach((key) => { outStr += currencyToString(key, currencies[key]); outStr += '\n'; }); return outStr; } function currencyToString(key, currency) { return `${key}, ${currency.name}, ${currency.value} ${currency.denomination}`; } console.log(compileCurrenciesString(currencyTypes));

Comments

0

You could use Object.keys(currencyTypes) to retrieve all of an objects keys, and then loop over those:

const currencyTypes = { NOK: {value:1.00000, name: "Norske kroner", denomination: "kr" }, EUR: {value:0.10733, name: "Europeiske euro", denomination: "€" }, USD: {value:0.12652, name: "United States dollar", denomination: "$" }, GBP: {value:0.09550, name: "Pound sterling", denomination: "£" }, }; Object.keys(currencyTypes).forEach(k => console.log( `${k}, ${currencyTypes[k].name}, ${currencyTypes[k].value} ${currencyTypes[k].denomination}` ));

Comments

0

You could use for...of loop with Object.entries to get key value.

var currencyTypes = { NOK: {value:1.00000, name: "Norske kroner", denomination: "kr" }, EUR: {value:0.10733, name: "Europeiske euro", denomination: "€" }, USD: {value:0.12652, name: "United States dollar", denomination: "$" }, GBP: {value:0.09550, name: "Pound sterling", denomination: "£" }}; for([key, {name, value, denomination}] of Object.entries(currencyTypes)) { let str = `${key}, ${name}, ${value} ${denomination}` console.log(str) }

Comments

0

If you're coding to the ES6 spec then you can use destructuring assignment.

I also utilize Object.entries method to turn the object properties into an array of key value entry pairs - then iterate over that with Array.prototype.forEach.

var currencyTypes = { NOK: { value: 1.00000, name: "Norske kroner", denomination: "kr" }, EUR: { value: 0.10733, name: "Europeiske euro", denomination: "€" }, USD: { value: 0.12652, name: "United States dollar", denomination: "$" }, GBP: { value: 0.09550, name: "Pound sterling", denomination: "£" }, }; Object.entries(currencyTypes).forEach(entry => { const [key, val] = entry; const { value, name, denomination } = val; console.log(`${key}\n${value}\n${name}\n${denomination}`); });

Comments

0

If you represent your collection as an array, it'll becomes easier to do this kind of things:

const currencyTypes = [ { "ticker": "NOK", "value":1.00000, "name": "Norske kroner", "denomination": "kr" }, { "ticker": "EUR", "value":0.10733, "name": "Europeiske euro", "denomination": "€" }, { "ticker": "USD", "value":0.12652, "name": "United States", "denomination": "$" }, { "ticker": "GBP", "value":0.09550, "name": "Pound sterling", "denomination": "£" } ]; // Amount of currencies: console.log( 'There are ' + currencyTypes.length + ' currencies in the array' ); // Array of all currency tickers: console.log( currencyTypes.map( currency => currency.ticker )); // Console.logging all of them: currencyTypes.forEach( currency => console.log( currency )); // Console.logging the properties using destructuring: currencyTypes.forEach(({ ticker, value, name, denomination }) => console.log( ticker, value, name, denomination ));

When you have multiples of something, arrays are usually the easiest way since most objects representing collections can be created from the array in one line of code.

If you want to stay with using an object, look at Object.keys(), Object.values() and Object.entries(). Those will do alot of the converting back and forth from objects to arrays.

The function you coded to get the amount of keys in the object, basically is the same as Object.keys():

const currencyTypes = { NOK: {value:1.00000, name: "Norske kroner", denomination: "kr" }, EUR: {value:0.10733, name: "Europeiske euro", denomination: "€" }, USD: {value:0.12652, name: "United States dollar", denomination: "$" }, GBP: {value:0.09550, name: "Pound sterling", denomination: "£" }, }; console.log( 'there are ' + Object.keys( currencyTypes ).length + ' currencies' );

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.