1

I have a JSON object like below and I want to retrieve values in array without neet to pass a key

 var myData={
  "VALUEX": "GrandTotal",
  "VALUEX2": "GrandTotal",
  "Jan-2018": 25,
  "Feb-2018": 54.5,
  "Mar-2018": 84,
  "Apr-2018": 45.2,
  "May-2018": 46,
  "Jun-2018": 76.5,
  "Jul-2018": 107,
  "Aug-2018": 138,
  "Sep-2018": 168.5,
  "Oct-2018": 199,
  "Nov-2018": 229.5,
  "Dec-2018": 260
}
H.Ostwal
  • 333
  • 1
  • 5
  • 11
  • 1
    `Object.values`? – CertainPerformance Nov 23 '18 at 08:27
  • 3
    Also, [there's no such thing as a "JSON Object"](http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/). If you have an object or array, then you have an object or array, full stop. JSON format is a *method of representing an object in a string*, like `const myJSON = '{"foo":"bar"}'`. If there are no strings, serialization, or deserialization involved, then JSON is not involved either. – CertainPerformance Nov 23 '18 at 08:27
  • Object.values() was perfect answer for me. I was looking for one line solution. Other ways I knew like below ------ function getJSONvalues(jsonObject){ var valuesArray=[]; Object.keys(jsonObject).forEach(function(key) { valuesArray.push(jsonObject[key]); }); return valuesArray; } – H.Ostwal Nov 25 '18 at 07:47

2 Answers2

12

There are several ways you can get the value of object without nowing the key:

Using Object.values()

var myData = {"VALUEX":"GrandTotal","VALUEX2":"GrandTotal","Jan-2018":25,"Feb-2018":54.5,"Mar-2018":84,"Apr-2018":45.2,"May-2018":46,"Jun-2018":76.5,"Jul-2018":107,"Aug-2018":138,"Sep-2018":168.5,"Oct-2018":199,"Nov-2018":229.5,"Dec-2018":260}
console.log(Object.values(myData));

Using Object.entries()

var myData = {"VALUEX":"GrandTotal","VALUEX2":"GrandTotal","Jan-2018":25,"Feb-2018":54.5,"Mar-2018":84,"Apr-2018":45.2,"May-2018":46,"Jun-2018":76.5,"Jul-2018":107,"Aug-2018":138,"Sep-2018":168.5,"Oct-2018":199,"Nov-2018":229.5,"Dec-2018":260}
var arrayVal = Object.entries(myData).map(item => item[1]);
console.log(arrayVal);

Using Object.keys()

var myData = {"VALUEX":"GrandTotal","VALUEX2":"GrandTotal","Jan-2018":25,"Feb-2018":54.5,"Mar-2018":84,"Apr-2018":45.2,"May-2018":46,"Jun-2018":76.5,"Jul-2018":107,"Aug-2018":138,"Sep-2018":168.5,"Oct-2018":199,"Nov-2018":229.5,"Dec-2018":260}
var arrayVal = [];
Object.keys(myData).forEach(key => arrayVal.push(myData[key]));
console.log(arrayVal);
Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62
  • Thanks. It worked for me. I had many alternatives but wanted short one like this. – H.Ostwal Nov 25 '18 at 07:42
  • I developed custom function like this function getJSONvalues(jsonObject){ var valuesArray=[]; Object.keys(jsonObject).forEach(function(key) { valuesArray.push(jsonObject[key]); }); return valuesArray; } – H.Ostwal Nov 25 '18 at 07:44
  • Which one is performant and better to use? – Rifat May 06 '21 at 12:12
0

You can just use the following notation:

myData["VALUEX"]

Which will translate to "GrantTotal".

To iterate between the keys and their values you can do the following:

for(var key in myData){
    if(myData.hasOwnProperty(key){
        console.log(myData[key]);
    } 
}
TiagoM
  • 3,458
  • 4
  • 42
  • 83