Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.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设置同一个键的s值_Javascript_Object - Fatal编程技术网

用其他对象替换对象值';使用JavaScript设置同一个键的s值

用其他对象替换对象值';使用JavaScript设置同一个键的s值,javascript,object,Javascript,Object,我有两个对象,item和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 我想做的正是以下几点: if (item.id == null || items

我有两个对象,
item
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
我想做的正是以下几点:

if (item.id == null || items.id == 0)
{
    item.id = results.id;
}
但是我正在为我的
对象的每个值寻找这样做的方法。你知道,如果我的对象碰巧有更多的键/值,就不必编写一个巨大的函数。 有什么想法吗

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

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

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

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

它可以做到这一点

var item={};
var结果={};
item.id='50'
item.area='墨西哥'
item.gender=null
item.birthdate=null
results.id='50'
results.area=null
结果:性别=‘男性’
results.birthdate=null
Object.key(item).forEach(function(key){
如果(项[键]==null | |项[键]==0){
项目[关键]=结果[关键];
}
})
document.getElementById('dbg').innerHTML=''+JSON.stringify(item,null',)+'';
console.dir(项目)

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

const target = { a: 1, b: 2 };
const source = { b: 4, c: 5 };

const returnedTarget = Object.assign(target, source);

console.log(target);
// expected output: Object { a: 1, b: 4, c: 5 }

console.log(returnedTarget);
// expected output: Object { a: 1, b: 4, c: 5 }
您可以优雅地使用:

var结果={};
var item={};
item.id='50';
项目面积='墨西哥';
item.gender=null;
item.birthdate=null;
results.id='50';
results.area=null;
结果:性别=‘男性’;
results.birthdate=null;
_.merge(结果、拾取(项、标识));
警报(JSON.stringify(results))
您也可以使用来简化它

var项={
id:'50',
地区:'墨西哥',
性别:空,
出生日期:空
};
var结果={
id:'50',
区域:空,
性别:'男性',
出生日期:空
};
Object.key(item).forEach(function(key){
项目[关键]=项目[关键]??结果[关键];
})
document.getElementById('dbg').innerHTML=''+JSON.stringify(item,null',)+'';
console.dir(项目)

以前的帖子,但是如果你搜索一个更简单的解决方案,你可以试试。如果源中的属性具有相同的键,则目标对象中的属性将被源中的属性覆盖


在对象上循环。或者,如果您正在使用jQuery,请查看merge。可能的重复项可以使用angulars
$filter
,或使用lodash,或使用本机的
.map
。选择你的毒药@谢谢你说的对。我以前见过那个问题,但误解了我自己的问题。
const target = { a: 1, b: 2 };
const source = { b: 4, c: 5 };

const returnedTarget = Object.assign(target, source);

console.log(target);
// expected output: Object { a: 1, b: 4, c: 5 }

console.log(returnedTarget);
// expected output: Object { a: 1, b: 4, c: 5 }