[英]Update matched key values in two JavaScript objects
My requirement is compare two objects and copy updated values into first object from second object.我的要求是比较两个对象并将更新的值从第二个对象复制到第一个对象中。 Ex :
例如:
$scope.obj1={"id" : 1, "name" : "java"}
$scope.obj2={"id" : 1, "name" : "java4you", "gender" : "male"}
compare(destination, bj1, obj2);
Destination variable Output:目标变量输出:
{"id" : 1, "name" : "java4you"}
The above two objects contains same keys but values are different.以上两个对象包含相同的键,但值不同。 I have to compare obj1 and obj2 and update with matched obj2 values
我必须比较 obj1 和 obj2 并使用匹配的 obj2 值进行更新
You can create a copy of obj1
using Object.assign()
in a new variable, destination
and iterate through each key of obj2
using Object.keys()
and array#forEach
and check if key exists in destination
, in case it exists, update the value in destination
from the value of obj2
您可以在新变量
destination
使用Object.assign()
创建obj1
的副本,并使用Object.keys()
和array#forEach
遍历obj2
每个键,并检查destination
中是否存在键,如果存在,则更新destination
来自obj2
的值
var obj1={"id" : 1, "name" : "java"}, obj2={"id" : 1, "name" : "java4you", "gender" : "male"} var updateObjectValue = (obj1, obj2) => { var destination = Object.assign({}, obj1); Object.keys(obj2).forEach(k => { if(k in destination) { destination[k] = obj2[k]; } }); return destination; } console.log(updateObjectValue(obj1, obj2));
Try this,试试这个,
function compare(obj1, obj2)
{
let obj = {};
for(let k in obj1)
{
obj[k] = obj2[k];
}
return obj;
}
Usage should be like,用法应该是这样的,
var destination = compare(obj1,obj2);
Without actually referring to methods that can be used in angularJs, You can try this:没有实际提到可以在 angularJs 中使用的方法,你可以试试这个:
function compare(o1, o2) { var biggest = o1; if (Object.keys(o2).length > Object.keys(o1).length) { biggest = o2; } for (let key in biggest) { if (!biggest.hasOwnProperty(key)) continue; if (o1[key] != o2[key]) { console.info("Difference in '"+ key +"': "+ o1[key] +" <> "+ o2[key]); } } } var $scope = {}; $scope.obj1 = {"id" : 1, "name" : "java"}; $scope.obj2 = {"id" : 1, "name" : "java4you", "gender" : "male"}; compare($scope.obj1, $scope.obj2);
But be careful using this, since it has many possible cases to fail.但是要小心使用它,因为它有很多可能失败的情况。
Using Angularjs, you can use .equal to compare and get the result, I don't see what is not working or providing your code will only gets you the answers.使用 Angularjs,您可以使用 .equal 进行比较并获得结果,我看不出什么不起作用,或者提供您的代码只会为您提供答案。
$scope.result = angular.equals($scope.obj1, $scope.obj2);
if($scope.result === true){
$scope.obj1 = $scope.obj2;
}
I had to update @Bharadwaj answer for it to work for me.我必须更新@Bharadwaj 的答案才能为我工作。
for (... in ...) statements must be filtered with an if statement for (... in ...) 语句必须用 if 语句过滤
var obj1={"id" : 1, "name" : "java"}, obj2={"id" : 1, "name" : "java4you", "gender" : "male"}; function compare(obj1, obj2) { let obj = {}; for(let k in obj1) { if(obj1[k] !== obj2[k]) { obj = Object.assign({}, obj1, obj1[k] = obj2[k]); } } return obj; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.