How to list objects including value, name, denomination?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
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
javascript object
add a comment |
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
javascript object
add a comment |
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
javascript object
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
javascript object
javascript object
asked Nov 16 '18 at 14:37
SenselessSenseless
427
427
add a comment |
add a comment |
6 Answers
6
active
oldest
votes
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");
}
add a comment |
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));
add a comment |
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}`
));
add a comment |
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)
}
add a comment |
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}`);
});
add a comment |
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' );
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53339924%2fhow-to-list-objects-including-value-name-denomination%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
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");
}
add a comment |
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");
}
add a comment |
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");
}
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");
}
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");
}
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");
}
answered Nov 16 '18 at 14:41
mbharanidharan88mbharanidharan88
4,11542455
4,11542455
add a comment |
add a comment |
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));
add a comment |
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));
add a comment |
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));
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));
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));
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));
answered Nov 16 '18 at 14:47
Daniil Andreyevich BaunovDaniil Andreyevich Baunov
1,137528
1,137528
add a comment |
add a comment |
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}`
));
add a comment |
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}`
));
add a comment |
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}`
));
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}`
));
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}`
));
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}`
));
answered Nov 16 '18 at 14:44
Olian04Olian04
2,21011137
2,21011137
add a comment |
add a comment |
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)
}
add a comment |
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)
}
add a comment |
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)
}
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)
}
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)
}
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)
}
answered Nov 16 '18 at 14:45
Nenad VracarNenad Vracar
73.3k126085
73.3k126085
add a comment |
add a comment |
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}`);
});
add a comment |
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}`);
});
add a comment |
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}`);
});
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}`);
});
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}`);
});
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}`);
});
answered Nov 16 '18 at 14:56
Tom O.Tom O.
2,71121428
2,71121428
add a comment |
add a comment |
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' );
add a comment |
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' );
add a comment |
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' );
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' );
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 ));
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 ));
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' );
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' );
edited Nov 16 '18 at 14:59
answered Nov 16 '18 at 14:52
ShillyShilly
5,9631717
5,9631717
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53339924%2fhow-to-list-objects-including-value-name-denomination%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown