0

Below is my JSON

[{"oppname":"BHP Deepwater Invictus returns to Tinidad","account":"BHP Petroleum (Trinidad 2C) Ltd.","contact":"Berky Ashcraft","title":"Sr. Drilling Engineer","doi":"2018-11-07","country_c":"Trinidad and Tobago","11-2018":"0","10-2018":"0","09-2018":"0","08-2018":"0","07-2018":"0","06-2018":"0","05-2018":"0","04-2018":"0","03-2018":"0","02-2018":"0","01-2018":"0","12-2017":"0"}]

As you can see I have some fields in the format of date ("11-2018" etc). How can I get these values in the front end AJAX call? I can get other values like this.oppname or this.account etc but this.11-08 doesn't work, neither this.(11-08). What is the proper way of getting these kind of field values?

Thanks.

vamsi
  • 1,488
  • 3
  • 28
  • 66

3 Answers3

0

To access the key which has - you need to use following pattern

this["11-2018"]
Ayaz Ali Shah
  • 3,453
  • 9
  • 36
  • 68
0

You need to use ECMAscripts "bracket notation":

var testData= [{"oppname":"BHP Deepwater Invictus returns to Tinidad","account":"BHP Petroleum (Trinidad 2C) Ltd.","contact":"Berky Ashcraft","title":"Sr. Drilling Engineer","doi":"2018-11-07","country_c":"Trinidad and Tobago","11-2018":"0","10-2018":"0","09-2018":"0","08-2018":"0","07-2018":"0","06-2018":"0","05-2018":"0","04-2018":"0","03-2018":"0","02-2018":"1","01-2018":"0","12-2017":"0"}];

console.log(testData[0]["09-2018"]);
console.log(testData[0]["02-2018"]);

Read more here

Just code
  • 13,553
  • 10
  • 51
  • 93
0

You can use the following code.

var jsonData = {"11-2018":"0","10-2018":"0","09-2018":"0","08-2018":"0","07-2018":"0","06-2018":"0","05-2018":"0","04-2018":"0","03-2018":"0","02-2018":"0","01-2018":"0","12-2017":"0"};


console.log(Object.keys(jsonData));

Object.keys(jsonData).map(function(arr) {
  console.log(jsonData[arr]);
})