简体   繁体   English

在Javascript中使用键值推送两个数组

[英]Push two array with key values in Javascript

I have two arrays first array connections Array 我有两个数组,第一个数组连接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
        } 
    ]
}

you want to create a new object, not an array: 您要创建一个新对象,而不是一个数组:

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

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 . 您不需要array.push

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM