i have and array
reservation1:[
{name:"8:30",active:true,dayindex:1},
{name:"jad",active:true,dayindex:3},
]
i need to expand the array to 9 and fill it with object with name null active false dayindex: between 0 and 10 in order the output needed is
output =[
{name:"",active:false,dayindex:0}
{name:"8:30",active:true,dayindex:1}
...
i tried this for expanding
it worked for expanding but i couldnt reorder it as i wanted
You can sort you array after adding others elements:
var reservation = [
{name:"8:30",active:true,dayindex:1},
{name:"jad",active:true,dayindex:3},
{name:"tony",active:true,dayindex:4},
{name:"",active:false,dayindex:6}
];
var availabeDayIndex = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9].filter(el => !reservation.map(it => it.dayindex).includes(el));
var sortField = 'dayindex';
reservation = [...reservation, ...Array.from(Array(9 - reservation.length), (val, key) => ({name: null, active: false, dayindex: availabeDayIndex[key]}))].sort((a, b) => a[sortField] - b[sortField]);
console.log(reservation);
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments