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 .

4
  • Where do the desired keys and array values come from? 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]} 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. 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 Commented Jan 26, 2017 at 18:44

5 Answers 5

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!

Sign up to request clarification or add additional context in comments.

2 Comments

it happens all the time, I was writing the answer at the same time than you, +1 for you =)
its just an difference of chrome and firefox console, both shows different things. see my answer below
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" }
]

Comments

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]
}

Comments

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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.