Home > Software design >  get specific value from object - Typescript
get specific value from object - Typescript

Time:07-11

In react application I have json response object that looks like this :

{
   100: "apple,
   200: "bananas",
   300: "pineapples"
 }

I want to get a specific value, for example, a value that corresponds to 200 - "bananas".

What is the easiest way to do this? thank you!

CodePudding user response:

const yourObject = {
   100: "apple",
   200: "bananas",
   300: "pineapples"
 }

console.log(yourObject[key])

CodePudding user response:

Assuming your JSON object is saved in myJson

const myJson = {
   100: "apple",
   200: "bananas",
   300: "pineapples"
 }

You would access bananas like that:

myJson[200]
  • Related