2

Simple problem here: 2 Json objects I would like to merge, while renaming the second.

First Array (obj1):

[  
   {  
      "name":"Metric 1",
      "value":33731487,
   },
   {  
      "name":"Metric 2",
      "value":11252893,
   }
]

Second Array (obj2):

[  
   {  
      "name":"Metric 1",
      "value":118181851,
   },
   {  
      "name":"Metric 2",
      "value":15151,
   }
]

Desired Result:

[  
   {  
      "name":"Metric 1", // Obj1
      "value":118181851, // Obj1
      "name_compare":"Metric 1", // Obj2
      "value_compare":148748, // Obj2
   },
   {  
      "name":"Metric 2", // Obj1
      "value":15151, // Obj1
      "name_compare":"Metric 2", // Obj2
      "value_compare":741178, // Obj2
   }
]

So I tried:

Renaming the Obj2 (works ok):

function JsonRename(obj) {
    var output = {};
    for (i in obj) {
        if (Object.prototype.toString.apply(obj[i]) === '[object Object]') {
            output[i] = JsonRename(obj[i]);
        } else {
            output[i+'_compare'] = obj[i];
        }
    }
    return output;
}

I've then tried to merge them by using:

function JsonMergeCompare(obj1, obj2 ) {

  var renamed_obj2 = JsonRename(obj2);

  var output = {};
  for (i in obj1) {

    output[i] = obj1[i];
    output[i] = renamed_obj2[i];

  }

  return output;

}

My problem is most certainly in the function above, because it only returns the obj2 (which makes sense as I'm iterating over output[i] twice) but how can I get in and change only the key -> values?

2
  • can you provide a fiddle or plunkr? Commented Feb 2, 2016 at 10:32
  • Why the -1? I think it's a fairly decent question! Commented Feb 2, 2016 at 13:12

3 Answers 3

4

I suggest you to define an Array.zip() function like this:

Array.zip = function(left, right, combinerFunction) {
    var counter, results = [];

    for(counter = 0; counter < Math.min(left.length, right.length); counter++) {
        results.push(combinerFunction(left[counter], right[counter]));
    }

    return results;
};

And then use it to zip the arrays together like this:

var resultArray = Array.zip(array1, array2, function(one, two) {
    return {
        name: one.name, 
        value: one.value,
        name_compare: two.name, 
        value_compare: two.value
    };
});
Sign up to request clarification or add additional context in comments.

Comments

1

As you say the function is the problem.

You need two for loops one per object as follows:

function JsonMergeCompare(obj1, obj2 ) {

  var renamed_obj2 = JsonRename(obj2);

  var output = {};
  for (i in obj1)
    output[i] = obj1[i];
  for (i in renamed_obj2)
    output[i] = renamed_obj2[i];
  return output;
}

Comments

0

try this one, I'm sure what is really you want but I just based my answer in your desired output

<script>
//First Array (obj1)
var _obj1=[  
   {  
      "name":"Metric 1",
      "value":33731487,
   },
   {  
      "name":"Metric 2",
      "value":11252893,
   }
];

//Second Array (obj2):
var _obj2=[  
   {  
      "name":"Metric 1",
      "value":118181851,
   },
   {  
      "name":"Metric 2",
      "value":15151,
   },
   {  
      "name":"Metric 3",
      "value":123,
   },

];
	function jsonRename(obj1,obj2){
		var out=[];
		var keys={};
		var ctr=0;
		var indx=0;
		for(v in obj1){
			  if(typeof(obj1[v].name)!="undefined" ){
				 var n=obj1[v].name;
				  if( typeof(keys[n])=="undefined"){
					out.push(obj1[v]);  
					keys[n]=out.length-1;	  
				  }
				  for(v1 in obj2){
					try{
					  if(n==obj2[v1].name){
						 ctr++;
						 out[keys[n]]["name_compare"+ctr]= obj2[v1].name;
						 out[keys[n]]["value_compare"+ctr]= obj2[v1].value;
					  }
					}catch(e){}
				  }
			  }
		}
		 return out;
	}
var out=jsonRename(_obj1,_obj2);

</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.