0

How to change the "nom_articole" key to the s value?

        var s = document.getElementById("myvalue").textContent;

        var obj = {
            url: "http://localhost:53698/mobile_ODataService.svc/",
            jsonp: true,
            errorHandler: function (error) {
                alert(error.message);
            },
            entities: {
                nom_articole/*I want here the s value in nom_articole place*/: { key: "id" },
            }
        };
Attila
  • 405
  • 1
  • 4
  • 6
  • possible duplicate of [Is it possible to add dynamically named properties to JavaScript object?](http://stackoverflow.com/questions/1184123/is-it-possible-to-add-dynamically-named-properties-to-javascript-object) – John Dvorak Sep 06 '13 at 12:48

2 Answers2

2

You can't use a variable as identifier in an object literal. You just can add another dynamic property after object creation:

var s = document.getElementById("myvalue").textContent;

var obj = {
  url: "http://localhost:53698/mobile_ODataService.svc/",
  jsonp: true,
  errorHandler: function (error) {
     alert(error.message);
  },
  entities : {}
};
obj.entities[s] = { key: "id" };
Sirko
  • 72,589
  • 19
  • 149
  • 183
0

You have to do this after the object creation.

var s = document.getElementById("myvalue").textContent;

var obj = {
    url: "http://localhost:53698/mobile_ODataService.svc/",
    jsonp: true,
    errorHandler: function (error) {
        alert(error.message);
    }
};
obj[s] = {
    nom_articole: { key: "id" },
};
Oskar Hane
  • 1,824
  • 13
  • 8