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 .
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 .
Share Improve this question asked Jan 26, 2017 at 15:30 MalikMalik 3,6907 gold badges30 silver badges42 bronze badges 4- Where do the desired keys and array values e from? – Matt Mokary Commented Jan 26, 2017 at 15:39
-
Assign to map is an object, that has a key of
123
whose value is an array:assignToMap = {123:[1,2,3,4]}
– Adam Jenkins Commented Jan 26, 2017 at 15:43 - firefox console always shows object of object, where as the image you mentioned above is of chrome, so its just a matter of browser. – DHRUV GUPTA Commented Jan 26, 2017 at 15:55
- thanks all. i wanted to do this operation using a loop dynamically. Also it was important assignToMap variable shouldn't be a array . Found this answer else where in stackoverflow – Malik Commented Jan 26, 2017 at 18:44
5 Answers
Reset to default 2To 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!
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" }
]
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]
}
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.
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.