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
}
3
  • 1
    Object.values? Commented Nov 23, 2018 at 8:27
  • 3
    Also, there's no such thing as a "JSON Object". 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. Commented Nov 23, 2018 at 8: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; } Commented Nov 25, 2018 at 7:47

2 Answers 2

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

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

3 Comments

Thanks. It worked for me. I had many alternatives but wanted short one like this.
I developed custom function like this function getJSONvalues(jsonObject){ var valuesArray=[]; Object.keys(jsonObject).forEach(function(key) { valuesArray.push(jsonObject[key]); }); return valuesArray; }
Which one is performant and better to use?
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]);
    } 
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.