Reputation: 3680
I have the following problem when i try to post a json object to a java API backend. The post method require an Object which contains list of objects.
Is it possible to create a list of objects inside a javascript Object . I understand that we can create a "Array" of objects inside a Object . But in my case , i need the data structure to be as follows .
if you notice , the object list inside the assignToMap
Object consists of key value pairs where the key is an integer and the value is an array . I did try , but all i could find as a solution was creating a array of objects inside a Object as follows .
Any help would be appreciated .
Upvotes: 0
Views: 7168
Reputation: 2091
Yes offcourse, it can be achieved easily.
see this below:-
var a = {};
a.assignToMap = [{
58343: [
22100,
2495
]
},
{
c: [
1, 2, 3
]
}
]
console.log(a)
if you do the above in firefox console, it will show you object of object whereas if you run same chrome console it will show you Array too. Dont worry its just a matter of browser.
Upvotes: 1
Reputation: 2398
If I understand your problem correctly you just need to use [] from your object. Try the following:
var assignToMap = {};
assignToMap[123] = [1, 2, 3, 4];
I'm guessing you were trying assignToMap.123
and were getting problems.
Upvotes: 1
Reputation: 4790
If I understood you, what you need it use " on integers on the keys to set it as keys:
assignToMap = {
"123" : [1,2,3,4],
"567" : [1,2,3,4]
}
Upvotes: 1
Reputation: 1571
To create the structure you mention you can write it in JSON like this:
{
"assignToMap": {
"123": [1, 2, 3, 4],
"345": [1, 2, 3, 4],
"678": [1, 2, 3, 4]
}
}
Although I'm not entirely sure if that's what you're asking here!
Upvotes: 2
Reputation: 6698
I believe you are talking about an associative array. Use the curly braces. This is an array of associative arrays.
var myArray = [
{ myKey1: "myValue1", myKey2: "myValue2", myKey3: "myValue3" },
{ myKey4: "myValue4", myKey5: "myValue5", myKey6: "myValue6" },
{ myKey7: "myValue7", myKey8: "myValue8", myKey9: "myValue9" }
]
Upvotes: 1