Fill a empty value of a key-value pair with the last value of the previous key.
myitems = {
'items1': [{first:true, second:false}, {first:true, second:true}],
'items2': [], //should be filled with {first:true, second:true}
'items3': [], //should be filled with {first:true, second:true}
'items4': [{first:true, second:false}, {first:true, second:true}],
'items5': [{first:false, second:true}],
'items6': [], //should be filled with {first:false, second:true}
'items7': [{first:true, second:true}],
}
>Solution :
You can do something like this to achieve that..
let lastValue = null;
for (const key in myitems) {
if (myitems[key].length === 0) {
if (lastValue !== null) {
myitems[key] = [lastValue];
}
} else {
lastValue = myitems[key][myitems[key].length - 1];
}
}