Mohaideen Ismail
Mohaideen Ismail

Reputation: 314

form the array value in javascript

I am trying to form the following structure

{
    "details": [
        {
            "id": 1,
            "index": "index"
        },
        {
            "id": 2,
            "index": "index"
        }
    ],
    "user": "user"
}

For that iam trying

 var order = [];
 $('.movables').each(function(index, element){
    var id = $(this).children().attr('id');
    order.push({'id': id, 'index' : index});
 });
 order.push('user',user);

It gives wrong format. I dont know how to form that above structure.

I am getting id, index value from each method. It is working fine. Iam getting id, index & user values but i dont know how to form

Upvotes: 1

Views: 47

Answers (1)

Ionică Bizău
Ionică Bizău

Reputation: 113375

Instead of an array, order should be an object, containing details property that is an array.

var order = {details: []};
$('.movables').each(function(index, element){
   var id = $(this).children().attr('id');
   order.details.push({'id': id, 'index' : index});
});
order.user = user;

Upvotes: 6

Related Questions