javascript - 在Javascript中推送两个带有键值的数组

标签 javascript arrays

我有两个数组,第一个数组 连接数组

[
    {
        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'
    }
]

计数数组

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

这里将第二个数组插入第一个数组

connections.push(count);

我的要求是如何将键值添加到连接的数组中,如下所示。我需要在每个数组之前添加帐户和计数键。请帮我找到一种解决方案。谢谢

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

最佳答案

你可以这样做

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

 (or)

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

您不需要array.push

关于javascript - 在Javascript中推送两个带有键值的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23101658/

相关文章:

javascript - Handlebars 不显示内容

javascript - 如何按顺序而不是随机地从数组中选择项目

arrays - 遍历数组,如果值存在则返回 TRUE

PHP PDO mySQL 查询返回列名和整数

c++ - 无法在 C++ 中声明动态二维数组

java - 在 Java 中调整矩阵的大小

javascript - Marklogic 服务器端 Javascript : XDMP-CONFLICTINGUPDATES while using explicit commit

javascript - Javascript 中允许的日期范围是什么?

javascript - 在 ckeditor 中为按键添加事件监听器代码

javascript - 在 D3 中换行长文本标签,无需额外换行