I have 3 object in array, for the first object i want to add ifContrbtd:false
and for remaining 2 objects i want to add ifContrbtd:true
. I have tried like following but it added ifContrbtd:true
to 2nd and 3rd object, nothing is added to first object.
for (var key in res.result) {
var ifContributed = false;
if(res.result[key].newField != undefined){
ifContributed = true;
res.result[key]['ifContrbtd']=ifContributed;
}
}
You should move the assignment outside the if
condition so that ifContributed = false
is assigned for objects having newField
. Right now nothing will be assined if it is defined:
for (var key in res.result) {
var ifContributed = false;
if (res.result[key].newField != undefined) {
ifContributed = true;
}
res.result[key]['ifContrbtd'] = ifContributed;
}
You have to put assign value to res.result[key]['ifContrbtd'] after the if statement not inside it.
Just below 3 lines will solve your issue:
for (var key in res.result) {
var ifContributed = (res.result[key].newField != undefined);
res.result[key]['ifContrbtd'] = ifContributed;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.