I've got two objects, item
and results
.
They've both got the same keys but possibly different values, for example:
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 item
object. You know, without having to write a huge function if my objects happen to have a lot more keys / values.
Any ideas?
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.
for (var key in item) {
if(item[key] == null || item[key] == 0){
item[key] = results[0][key]
}
}
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With