0

I have a JSON object of the following type:

{ 
ONE: 
   { id: 7,
     first: '0.000001',
     last: '0.00000017'},
TWO: 
   { id: 8,
     first: '0.000002',
     last: '0.00000027'},
THREE: 
   { id: 9,
     first: '0.000003',
     last: '0.00000037'},
FOUR: 
   { id: 10,
     first: '0.000004',
     last: '0.00000047'}
}

And I'm looking to search for the right name (ONE for example) and use that object. I've tried using hasOwnProperty('ONE') and found the actual property, but I don't understand how to access the data within it. Keep in mind, I'm getting 'ONE' from a variable, so I couldn't just do something like this:

var property='ONE'
var temp = JSON.parse(obj)
console.log(temp.property.id)
console.log(temp.property.first)
console.log(temp.property.last)

How can I "dynamically" find the property im looking for and access the data within it?

Gal Appelbaum
  • 1,905
  • 6
  • 21
  • 33
  • JSON is a *textual notation* for data exchange. [(More here.)](http://stackoverflow.com/a/2904181/157247) If you're dealing with JavaScript source code, and not dealing with a *string*, you're not dealing with JSON. – T.J. Crowder Dec 19 '17 at 15:04

1 Answers1

2

You can access them this way:

var property='ONE'
var temp = JSON.parse(obj)
console.log(temp[property].id)
//...
mehulmpt
  • 15,861
  • 12
  • 48
  • 88