admin管理员组文章数量:1291083
I have a object like this code
var coinNameKR = {
BTC: 'bitcoin',
ETH: 'ethereum',
DASH: 'dash',
}
And I want to get each value's with the key. So I search on stackoverflow and find this code
function getValueByKey(object, row) {
return Object.values(object).find(x => object[x] === row.key);
}
console.log(getValueByKey(coinNameKR, row.key));
But It seems it only returns bitcoin only.
For example if
console.log(getValueByKey(coinNameKR, 'ETH'));
it should be ethereum, but still bitcoin. And I found get Key By Value but I can't not find get value by key.
I have a object like this code
var coinNameKR = {
BTC: 'bitcoin',
ETH: 'ethereum',
DASH: 'dash',
}
And I want to get each value's with the key. So I search on stackoverflow and find this code
function getValueByKey(object, row) {
return Object.values(object).find(x => object[x] === row.key);
}
console.log(getValueByKey(coinNameKR, row.key));
But It seems it only returns bitcoin only.
For example if
console.log(getValueByKey(coinNameKR, 'ETH'));
it should be ethereum, but still bitcoin. And I found get Key By Value but I can't not find get value by key.
Share Improve this question edited Jul 2, 2019 at 2:26 Jack Bashford 44.1k11 gold badges55 silver badges82 bronze badges asked Jul 2, 2019 at 2:24 writingdeveloperwritingdeveloper 1,0764 gold badges24 silver badges52 bronze badges3 Answers
Reset to default 4You just need to return the value of the key in the object:
var coinNameKR = {
BTC: 'bitcoin',
ETH: 'ethereum',
DASH: 'dash',
}
function getValueByKey(object, row) {
return object[row];
}
console.log(getValueByKey(coinNameKR, "ETH"));
Is that is what you are looking for?
var coinNameKR = {
BTC: 'bitcoin',
ETH: 'ethereum',
DASH: 'dash',
}
for(let i in coinNameKR){
console.log(`${i} has the value: ${coinNameKR[i]}`)
}
var coinNameKR = {
BTC: 'bitcoin',
ETH: 'ethereum',
DASH: 'dash',
}
const dumpProps = obj => Object.keys(obj).forEach(key => { console.log(`${key}'s value is ${obj[key]}`) });
dumpProps(coinNameKR);
本文标签: How to get javascript object39s value with keyStack Overflow
版权声明:本文标题:How to get javascript object's value with key - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741524672a2383396.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论