0

I have two arrays first array connections Array

[
    {
        account_id: '1000024965925119facebook',
        account_name: 'JijoJohn',
        account_image: '5133_a.jpg',
        account_type: 'facebook'
    },
    {
        account_id: '100002496592511facebook',
        account_name: 'JijoJohn',
        account_image: '55133_a.jpg',
        account_type: 'facebook'
    },
    {
        account_id: '138115932930527facebook',
        account_name: 'JijoJohn',
        account_image: '5555133_a.jpg',
        account_type: 'facebook'
    },
    {
        account_id: '101706396580621facebook',
        account_name: 'JijoJohn',
        account_image: '55133_a.jpg',
        account_type: 'facebook'
    },
    {
        account_id: 'feed1',
        account_name: 'JijoJohn',
        account_image: '5555133_a.jpg'
    },
    {
        account_id: '57880525twitter',
        account_name: 'MinuJose',
        account_image: 'b4_normal.png',
        account_type: 'twitter'
    }
]

count Array

[
    {
        type: 'facebook',
        count: 4
    },
    {
        type: 'twitter',
        count: 1
    }
]

Here am pushing the second array into first array

connections.push(count);

My requirement is how to add key values in to the joined array like the following way. I need to add accounts and count keys before each array. Please help me to find one solution. Thanks

{ 
    "accounts" : 
    [
        {
            account_id: '57880525twitter',
            account_name: 'MinuJose',
            account_image: 'b4_normal.png',
            account_type: 'twitter'
        },
        {
              account_id: '57880525twitter',
            account_name: 'MinuJose',
            account_image: 'b4_normal.png',
            account_type: 'twitter'
        }
    ],
    "count" :
    [

        {
            "type":"facebook",
             "count": 4
        } ,
         {
            "type":"twitter",
            "count": 1
        } 
    ]
}
1
  • I think there's something wrong with your data structure. When you call connections.push(count); it will not concatenate two arrays, it will just add an array in the end of the array. For instance : a=[1,2];a.push([3,4]); will produce [1,2,[3,4]], not [1,2,3,4]. Then I'm not sure what kind of data you want to join nor what the result should look like. Maybe you want to add two twitter accounts and update the accounts field of connections and update its count field ? Commented Apr 16, 2014 at 6:44

2 Answers 2

1

You can do it like

 var result = {
    accounts: accountsArray,
    counts: countsArray
 }

 (or)

 var result = {
 }
 result.accounts = accountsArray;
 result.counts = countsArray.

You do not need array.push.

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

Comments

1

you want to create a new object, not an array:

  new_object = {
     "accounts" : account_array,
     "count": count_array
  }

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.