1

I have the below

$scope.Marketing = [{
    'ProductId':1,
    'ProductName':'Product 1',
    'ProductDescription':'Product Description 1'
  },
  {
    'ProductId':2,
    'ProductName':'Product 2',
    'ProductDescription':'Product Description 2'
  }];

  $scope.Finance=[{
    'ProductId':1,
    'Price':'$200.00'
  },
  {
    'ProductId':2,
    'Price':'$100.00'
  }];

  $scope.Inventory=[{
    'ProductId':1,
    'StockinHand:':26
  },
  {
    'ProductId':2,
    'StockinHand':40
  }];

I want the output to be

enter image description here

My Merge function is here

$scope.tempresult=merge($scope.Marketing,$scope.Finance);  
 $scope.result=merge($scope.tempresult,$scope.Inventory);

function merge(obj1,obj2){ // Our merge function
var result = {}; // return result
for(var i in obj1){      // for every property in obj1 
    if((i in obj2) && (typeof obj1[i] === "object") && (i !== null)){
        result[i] = merge(obj1[i],obj2[i]); // if it's an object, merge   
    }else{
       result[i] = obj1[i]; // add it to result
    }
}
for(i in obj2){ // add the remaining properties from object 2
    if(i in result){ //conflict
        continue;
    }
    result[i] = obj2[i];
}

return result;

}

But the output is

enter image description here

The value for first Stock In Hand is missing.

What is the mistake I am making?

Edit

enter image description here

enter image description here

4
  • "What is the mistake I am making?" - you didn't try to debug it. big mistake. Commented Mar 21, 2015 at 10:56
  • They are all arrays.. You probably want to concatenate instead. Commented Mar 21, 2015 at 11:00
  • @techfoobar , I just tried return a.concat(b).concat(c); but it is not the case. I have to concatenate based on the common property which is ProductId here. Commented Mar 21, 2015 at 11:31
  • You'll need a more sophisticated concat. By arrays, i meant that $scope.Marketing, $scope. Finance and $scope. Inventory are just arrays containing the actual objects you need to merge. Commented Mar 21, 2015 at 11:34

2 Answers 2

1

you could use Jquery.extend property, have a look at the plnkr code

$scope.result=merge($scope.Marketing, $scope.Finance,$scope.Inventory);

function merge(obj1,obj2, obj3){
    return $.extend(true, obj1,obj2,obj3)
}
};

http://plnkr.co/edit/gKQ9bc?p=preview

Sign up to request clarification or add additional context in comments.

7 Comments

Check your 'StockinHand:' it should be without : like 'StockinHand'
Thanks a lot....very silly mistake. 2 questions i have for u. a) Using the Jquery.extend how the common properties are attached b) Ur code is not displaying any data. - why? But I am accepting ur (: But please answer.
Also as u can make out, my function accepts 2 array objects. If i have to merge more than that, then I have two call that function multiple times. Any better way to optimize my code...like the way JQuery.Extend has been done...u don't have to write the code... give me some steps and allow me to try out first.
Using jquery extend you can add n number of arrays, more info at api.jquery.com/jquery.extend
that's right but the plnkr.co/edit/gKQ9bc?p=preview is not working. It is just showing "Product list!"
|
1

One approach is populating the one array of products with missing properties from the other two (after matching with product id).

See: http://jsfiddle.net/zekbxh90/1/ - and check the console output

Code:

var a = [{
    'ProductId': 1,
        'ProductName': 'Product 1',
        'ProductDescription': 'Product Description 1'
}, {
    'ProductId': 2,
        'ProductName': 'Product 2',
        'ProductDescription': 'Product Description 2'
}];

var b = [{
    'ProductId': 1,
        'Price': '$200.00'
}, {
    'ProductId': 2,
        'Price': '$100.00'
}];

var c = [{
    'ProductId': 1,
        'StockinHand:': 26
}, {
    'ProductId': 2,
        'StockinHand': 40
}];

// lets add the props from b abd c into a to get the desired result
a.forEach(function (_itemA) {

    // get product id in a
    var productId = _itemA.ProductId,
        matching = false,
        prop = false;

    // get the matching item in b and add new props to a
    b.forEach(function (_itemB) {
        if (_itemB.ProductId === productId) merge(_itemA, _itemB);
    });

    // get the matching item in c and add new props to a
    c.forEach(function (_itemC) {
        if (_itemC.ProductId === productId) merge(_itemA, _itemC);
    });

});

console.log(a);

function merge(_to, _from) {
    for (var prop in _from) {
        if (!_to.hasOwnProperty(prop) && _from.hasOwnProperty(prop)) _to[prop] = _from[prop];
    }
}

4 Comments

hi, i just updated my merge function and only the first "Stock in hand" value is not coming. Could u please help me in that.
The fiddle and code I posted should be sufficient get you going as they are filly functional. Check the console after opening the fiddle.
Sir, your solution is very good and it is as per the way I asked. But what is _itemA.ProductId or _itemB.ProductId? I mean that property (ProductdId). Nevermind, I am new to JS world. If they are the properties that i have exposed in the array, so if I will change the name of the property, do i have to equally change that also.
The first argument in the callback (_itemA, _itemB, etc.) refers to each element in the array on which forEach() is called. Docs: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…

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.