0
currentid = 77;
produktid = 2222;
name = "bla";

$.extend(artikel,{
    currentid:{"produktid":produktid, "name":name}
});


var myJsonString = JSON.stringify(artikel);
console.log(myJsonString);

The result in the console is:

{"currentid":{"produktid":2222,"name":"bla"}}

But is must be:

{"77":{"produktid":2222,"name":"bla"}} 

or

{77:{"produktid":2222,"name":"bla"}}

How can I do this?

Eddy Unruh
  • 576
  • 1
  • 5
  • 18

2 Answers2

1

You're going to have to add the property using associative array syntax like this.

currentid = 77;
produktid = 2222;
name = "bla";
obj = {};
obj[currentId] = {"produktid": produktid, "name": name};

$.extend(artikel, obj);
var myJsonString = JSON.stringify(artikel);
console.log(myJsonString);
kamoroso94
  • 1,713
  • 1
  • 16
  • 19
0

Use computed property names

currentid = 77;
produktid = 2222;
name = "bla";

$.extend(artikel,{
    [currentid]:{"produktid":produktid, "name":name}
});
guest271314
  • 1
  • 15
  • 104
  • 177