使用 JavaScript 将对象值替换为相同键的其他对象的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33041524/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 14:33:39  来源:igfitidea点击:

Replace object value with other object's value of the same key with JavaScript

javascriptobject

提问by Luuk Gortzak

I've got two objects, itemand results. They've both got the same keys but possibly different values, for example:

我有两个对象,itemresults. 他们都有相同的键,但可能有不同的值,例如:

item.id = '50'
item.area = 'Mexico'
item.gender = null
item.birthdate = null

results.id = '50'
results.area = null
results.gender = 'Male' 
results.birthdate = null

What I want to do is exactly the following:

我想要做的正是以下内容:

if (item.id == null || items.id == 0)
{
    item.id = results.id;
}

but I'm looking for a way to do this for each value of my itemobject. You know, without having to write a huge function if my objects happen to have a lot more keys / values. Any ideas?

但我正在寻找一种方法来为我的item对象的每个值执行此操作。你知道,如果我的对象碰巧有更多的键/值,则不必编写一个巨大的函数。有任何想法吗?

Update :I misunderstood my own problem and the only issue was that I didnt really understand how to get an object value given a certain key. I couldnt really use any outside scripts or divs since Im using Azure's mobile service scripts.

更新:我误解了我自己的问题,唯一的问题是我真的不明白如何在给定某个键的情况下获取对象值。我无法真正使用任何外部脚本或 div,因为我使用 Azure 的移动服务脚本。

for (var key in item) {
    if(item[key] == null || item[key] == 0){
        item[key] = results[0][key] 
    }             
}

回答by Anonymous0day

It could do the trick !

它可以做到这一点!

var item = {};
var results={};

item.id = '50'
item.area = 'Mexico'
item.gender = null
item.birthdate = null

results.id = '50'
results.area = null
results.gender = 'Male'
results.birthdate = null

Object.keys(item).forEach(function(key) {
  if (item[key] == null || item[key] == 0) {
    item[key] = results[key];
  }
})
document.getElementById('dbg').innerHTML ='<pre>' + JSON.stringify(item , null , ' ') + '</pre>';

console.dir(item);
<div id='dbg'></div>

回答by AtheistP3ace

You can loop over the object like this. hasOwnProperty tests if it is a property defined by you and not from the base object definition.

您可以像这样循环对象。hasOwnProperty 测试它是否是您定义的属性,而不是来自基础对象定义的属性。

for (var key in item) {
   if (item.hasOwnProperty(key)) {
       if (item[key] == null) {
           item[key] = results[key];
       }
   }
}

回答by Meir

You can elegantly use lodash:

你可以优雅地使用lodash

var results = {};
var item = {};

item.id = '50';
item.area = 'Mexico';
item.gender = null;
item.birthdate = null;

results.id = '50';
results.area = null;
results.gender = 'Male'; 
results.birthdate = null;

_.merge(results, _.pick(item, _.identity));

alert(JSON.stringify(results));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.js"></script>

Note that the requested value is now in results (and not in item). If you still need it item, clone the values into a new variable and use it.

请注意,请求的值现在在结果中(而不是在项目中)。如果您仍然需要它,请将值克隆到一个新变量中并使用它。

回答by Dory Zidon

You could just iterate all the object keys, and then write assign them on each item:

您可以迭代所有对象键,然后在每个项目上写入分配它们:

for (var property in results) {
    if (results.hasOwnProperty(property) && !item[property]) {
        // do stuff
        item[property] = results[property]
    }
}