简体   繁体   English

更新两个 JavaScript 对象中匹配的键值

[英]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.

相关问题 合并两个对象的值,如果匹配则增加键的值[Javascript] - merge the value of two objects increasing the value of a key if matched [Javascript] 如何返回一个对象数组,返回与某些特定键匹配的对象:来自两个不同对象的值? - how to return an array of objects returning the objects that matched some specific key:values from two different objects? 通过关键Javascript合并两个对象(不覆盖不同的值) - merge two objects by key Javascript (without overriding different values) Javascript:如何合并两个对象并对同一个键的值求和? - Javascript: How to merge two objects and sum the values of the same key? 合并两个Javascript对象,其中每个键包含多个值 - Merging two Javascript objects where each key contains multiple values 比较 JavaScript 中的两个对象数组并通过比较键更新值 - Comparing two array of objects in JavaScript and update the values by comparing Keys 比较两个对象,仅返回第一个对象的匹配值 - Compare two objects and return only the matched values from the first object 获取javascript中两个对象数组之间匹配元素的索引 - get index of matched element between two array of objects in javascript 在Javascript中将对象作为键值的数组 - Arrays with Objects as Key Values in Javascript Javascript 中的对象中缺少键和值 - missing key and values in Objects in Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM