0

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 .

list of objects inside an Object

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 .

array of objects inside an Object - what i could achieve

Any help would be appreciated .

Malik
  • 3,520
  • 7
  • 29
  • 42
  • Where do the desired keys and array values come from? – Matt Mokary Jan 26 '17 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 Jan 26 '17 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 Jan 26 '17 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](http://stackoverflow.com/a/7255113/1305026) – Malik Jan 26 '17 at 18:44

5 Answers5

2

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!

iamalismith
  • 1,531
  • 9
  • 22
1

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" }
]
Matt Spinks
  • 6,380
  • 3
  • 28
  • 47
1

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]
}
Adriano Spadoni
  • 4,540
  • 1
  • 25
  • 25
1

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.

Andrew Wynham
  • 2,310
  • 21
  • 25
1

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.

for chrome

firefox

DHRUV GUPTA
  • 2,000
  • 1
  • 15
  • 24