JS controller
OUTPUT
input of json
input json
getting out not expected
output json
var lst = JSON.parse(JSON.stringify(component.get('v.lstPetval')));
var lstInsert = [];
var lstdel = JSON.parse(JSON.stringify(component.get('v.lstPetDeletedVal')));
var lstPetUpdateVal = [];
for(var i=0; i<lst.length; i++) {
lstInsert.push(lst[i]);
if(lst[i].objAddOns.hasOwnProperty('Id')) {
//console.log('lst[i]', lst[i]);
lstPetUpdateVal.push(lst[i]);
}
// if(lst[i].objAddOns.hasOwnProperty('Id')) {
// lstInsert.splice(i, 1);
// }
}
console.log('lstInsert', lstInsert);
for(var i=0; i<lst.length; i++) {
// console.log('lst[i]123', lst[i]);
// if(lst[i].objAddOns.hasOwnProperty('Id')) {
// console.log('lst[i]', lst[i]);
// console.log('lst[i]', lst.indexOf(lst[i]));
lstInsert.splice(i, 1);
// }
// if(lst[i].objAddOns.hasOwnProperty('Id')) {
//
// }
}
if(lstInsert.length == 0){
lstInsert = null;
}
if(lstdel.length == 0){
lstdel = null;
}
if(lstPetUpdateVal.length == 0){
lstPetUpdateVal = null;
}
console.log('lstInsert',lstInsert,'lstdel',lstdel,'lstPetUpdateVal',lstPetUpdateVal);
[![Here is the screen shot of input and output of JSON][2]][2][![output][2]][2]
Best Answer
The reason is splice has three parameters and first one is the index , second one is how many elements to remove and finally items to be added . In your case , you have given only two parameters and the system is removing (0,2,4 indexes from original array) Hence you are seeing only half the values.