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 badges
Add a ment  | 

3 Answers 3

Reset to default 4

You 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