在数组对象中分组

时间:2016-04-27 17:25:22

标签: javascript jquery html arrays

我有2个以下格式的javascript数组对象:

 var input1 = 
    {
      "a" : [
       {
         "id" : 1,
         "name" : "gh"
       }],
      "b" : [
       {
         "id" : 2,
         "name" : "ab"
       }]

    }

var input2 =
{
 "a" : [
       {
         "id" : 3,
         "name" : "cd"
       }],
       "b" : [
       {
         "id" : 4,
         "name" : "gh"
       }]
}

我希望通过以下方式对其进行格式化:

var finaloutput = 
    {
      "a" : [
       {
         "id" : 1,
         "name" : "gh"
       },
        {
         "id" : 3,
         "name" : "cd"
       }
        ],
      "b" : [
       {
         "id" : 2,
         "name" : "ab"
       },
       {
         "id" : 4,
         "name" : "gh"
       }
      ]      
    }

我试图在不使用任何外部库的情况下对数组进行分组。不太熟悉javascript中的linq,任何可用的内置函数或任何引用?

编辑:我在输入中进行了更改,因为这是我期待的输入

3 个答案:

答案 0 :(得分:2)

试试这个,当然这只是一个快速的实现 - 但它应该让你走上正确的道路:

var finaloutput = Object.keys(input1).reduce((p,v) => {
  p[v] = (input1[v].concat(input2[v]))
  return p;
}, {})

return =>

JSON.stringify(finaloutput,null,2);

"{
  "a": [
    {
      "id": 1,
      "name": "gh"
    },
    {
      "id": 3,
      "name": "cd"
    }
  ],
  "b": [
    {
      "id": 2,
      "name": "ab"
    },
    {
      "id": 4,
      "name": "gh"
    }
  ]
}"

答案 1 :(得分:1)

使用任意给定数量的输入

创建一个地图并填写它以检查密钥是否存在
lmm <- lmer(lnTriNU ~ Campaign + Watering + Heating + Campaign*Watering
       + Campaign*Heating + Watering*Heating + Campaign*Watering*Heating 
       + (1|Genotype) + (1|Week:Plant_id), pg)

答案 2 :(得分:0)

    var finalOutput;
    var input1 = {
            "a": [{
                    "id": 1,
                    "name": "gh"
            }],
            "b": [{
                    "id": 2,
                    "name": "ab"
            }]
    };

    var input2 = {
            "a": [{
                    "id": 3,
                    "name": "cd"
            }],
            "b": [{
                    "id": 4,
                    "name": "gh"
            }]
    };
    input1.a.push(input2.a[0]);
    input1.b.push(input2.b[0]);
    finalOutput = input1;

    //////////////////////////////////////////////////////    
    If the arrays are dynamic you can do the operation as follows:

    var input1 = {
        "a": [
          {
                "id": 1,
                "name": "gh"
          }
        ],
        "b": [
          {
                "id": 2,
                "name": "ab"
          }
        ]
}

var input2 = {
        "a": [
          {
                "id": 3,
                "name": "cd"
          }
        ],
        "b": [
          {
                "id": 4,
                "name": "gh"
          }
        ]
}
var inputNewLengthA = input1.a.length;
var inputNewLengthB = input1.b.length;
var finalOutput;
for(var v=0;v<inputNewLengthA;v++){
    input1.a.push(input2.a[v]);
}
for(var n=0;n<inputNewLengthB;n++){
    input1.b.push(input2.b[n]);
}
finalOutput = input1;
///////////////////////////////////////
Note: You can also use one for loop only if your array lengths are   
equal.