0

I have 2 javascript array objects of the following format:

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

    }

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

I am looking to format it in the following way :

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

I am trying to group the array without using any external libraries. Not very much familiar with linq in javascript , any built in functions available or any references?

Edit: I made the changes in the inputs since this is what I am expecting as inputs

2
  • 2
    Your first block doesn't hold what you think it does, the first "a","b" properties are going to be overwritten by the second set. So its just going to be { a:[{id:3}], b:[{id:4}] } Commented Apr 27, 2016 at 17:29
  • your object reinitialise the keys. Commented Apr 27, 2016 at 17:30

3 Answers 3

2

Try this, of course this is just a quick implemention - but it should set you on the correct path:

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

returns =>

JSON.stringify(finaloutput, null, 2);

"{
  "a": [
    {
      "id": 1,
      "name": "gh"
    },
    {
      "id": 3,
      "name": "cd"
    }
  ],
  "b": [
    {
      "id": 2,
      "name": "ab"
    },
    {
      "id": 4,
      "name": "gh"
    }
  ]
}"
Sign up to request clarification or add additional context in comments.

Comments

1

Create a map and fill it checking for key existence, with any given number of inputs

var allInputs = [];
allInputs.push(input1);
allInputs.push(input2);

var result = [];

for(var x in allInputs)
{
    var inp = allInputs[x];
    for(var i in inp)
    {
        if(result[i] == undefined)
        {
            result[i] = [];
        }

        result[i].push(inp[i]);
    }
}

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.