簡體   English   中英

在Javascript中使用鍵值推送兩個數組

[英]Push two array with key values in Javascript

我有兩個數組,第一個數組連接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'
    }
]

計數數組

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

您要創建一個新對象,而不是一個數組:

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

你可以像

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

 (or)

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

您不需要array.push

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM