Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用JavaScript合并对象?_Javascript - Fatal编程技术网

如何使用JavaScript合并对象?

如何使用JavaScript合并对象?,javascript,Javascript,我有下面的 $scope.Marketing = [{ 'ProductId':1, 'ProductName':'Product 1', 'ProductDescription':'Product Description 1' }, { 'ProductId':2, 'ProductName':'Product 2', 'ProductDescription':'Product Description 2' }]; $scope

我有下面的

$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
  }];
我希望输出是

我的合并函数在这里

$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;
}

但结果是

缺少第一手库存的值


我犯了什么错误

编辑


一种方法是填充另两个产品中缺少属性的一个产品数组(在与产品id匹配后)

请参阅:-并检查控制台输出

代码:


您可以使用Jquery.extend属性,看看plnkr代码

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

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

“我犯了什么错误?”-您没有尝试调试它。大错误。它们都是数组。。您可能想要连接。@techfoobar,我刚刚尝试返回a.concat(b).concat(c);但事实并非如此。我必须基于公共属性ProductId进行连接。您需要一个更复杂的concat。我所说的数组是指
$scope.Marketing
$scope。财务
$scope。库存
只是包含需要合并的实际对象的数组。您好,我刚刚更新了合并功能,只有第一个“库存”值没有出现。你能帮我一下吗?我贴的小提琴和代码应该足够让你走了,因为它们功能齐全。打开提琴后检查控制台。先生,您的解决方案非常好,并且符合我的要求。但是什么是_itemA.ProductId或_itemB.ProductId?我指的是那个财产。没关系,我对JS的世界还不熟悉。如果它们是我在数组中公开的属性,那么如果我要更改属性的名称,我也必须同样地更改它们。回调中的第一个参数(_itemA,_itemB,等等)引用数组中调用
forEach()
的每个元素。医生:检查你的“StockinHand:”应该没有:比如“StockinHand”,非常感谢……非常愚蠢的错误。我有两个问题要问你。a) 使用Jquery.extend如何附加公共属性b)Ur代码未显示任何数据。-为什么?但我接受你的建议(:但是请回答。正如你所知道的,我的函数接受2个数组对象。如果我必须合并更多的数组对象,那么我有两个函数可以多次调用该函数。有没有更好的方法来优化我的代码…比如JQuery.Extend已经完成了…你不必编写代码…给我一些步骤,让我先试用一下。使用JQuery Extent你可以添加n个数组,更多的信息是正确的,但是没有工作。它只是显示“产品列表!”
$scope.result=merge($scope.Marketing, $scope.Finance,$scope.Inventory);

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