2

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.

3 Answers 3

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"));

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

3 Comments

Thanks for answer. Solutions are really easy.. I think I should study more about javascript basic..
No worries @sangumee, always glad to help.
If all you are looking for is the value of a key why would you not just use coinNameKR.ETH or coinNameKR['ETH']?
1

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]}`)
}

Comments

1

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);

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.