1

The data which I'm getting from API is in this format having few keys in dot format and rest in normal. As javascript don't allow variable names having dot notation. How can I initialize variable names in dot notation ?

[{  
      "s.no":0,
      "amt.pledged":15823,
      "currency":"cad",
      "end.time":"2016-11-01T23:59:00-04:00",
}]

`

Afrin Athar
  • 83
  • 2
  • 8
  • You can use `.` in your object property name if you use a string as property name, e.g. `var obj = {}; obj['wow.cool'] = 'test'; console.log(obj["wow.cool"]);` – Tholle Nov 11 '18 at 18:42

2 Answers2

0

You can do something like below to create or initialize an object with keys containing .notation

    const obj = {};
    obj["s.no"] = 0;
    obj["amt.pledged"] = 15823;
    obj["currency"] = "cad";
    obj["end.time"] = "2016-11-01T23:59:00-04:00";

And while reading key values

   console.log(obj["amt.pledged"]);//this will print 15823
Hemadri Dasari
  • 32,666
  • 37
  • 119
  • 162
0
const object = [{
  "s.no": 0,
  "amt.pledged": 15823,
  "currency": "cad",
  "end.time": "2016-11-01T23:59:00-04:00",
}];
console.log(object[0]["amt.pledged"]); // 15823

object[0]["amt.pledged"] = 1000;
console.log(object[0]["amt.pledged"]);  // 1000

or

const object = {
  "s.no": 0,
  "amt.pledged": 15823,
  "currency": "cad",
  "end.time": "2016-11-01T23:59:00-04:00",
};
console.log(object["amt.pledged"]); // 15823

object["amt.pledged"] = 1000;
console.log(object["amt.pledged"]); // 1000
Boris Traljić
  • 956
  • 1
  • 10
  • 16