1

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

2
  • Discuss this on chat? Commented Nov 2, 2017 at 14:22
  • Yh please sir ... Commented Nov 2, 2017 at 14:24

1 Answer 1

1

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);

Sign up to request clarification or add additional context in comments.

2 Comments

dayindex to to be a variable it is changeable
dayindex will vary from 0 to 10 filling the required position here dayindex need to fill {0 2 5 7 8 9 }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.