-1

I have the following json object being returned and stored in a variable called data:

{"data":{"min":1,"q1":59,"median":117,"q3":175,"max":233}}

Since this is a single object and not an array of objects, I cannot do data[0], data[1] etc..

How can I pull out the min property and its value and store in a variable?

I've tried var test = JSON.parse(data) and then array.push([test.min]) but have failed.

Is there an easier way to do this?

user1156596
  • 601
  • 1
  • 10
  • 29
  • 3
    `test.data.min` – Weedoze Nov 21 '16 at 14:23
  • 1
    Is the "JSON object" a string, or is it a JavaScript object? If a string, `JSON.parse` it into an JavaScript object first. Then use standard object property access syntax (`obj.prop`) to get its properties. –  Nov 21 '16 at 14:25

2 Answers2

1

var obj = {
   "data": {
      "min": 1,
      "q1": 59,
      "median": 117,
      "q3": 175,
      "max": 233
   }
};
console.log(obj["data"]["min"]);
ozil
  • 6,930
  • 9
  • 33
  • 56
Harminder
  • 141
  • 1
  • 8
0

I have made a little function you can use :)

const data = {
  "data": {
    "min": 1,
    "q1": 59,
    "median": 117,
    "q3": 175,
    "max": 233
  }
}

function getValue(key, prop) {
  return data[key][prop]
}

const min = getValue('data', 'min')

console.log(min)

`

Jackthomson
  • 644
  • 8
  • 23