expand and reorder an array of objects

jadlmir

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

Faly

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.

edited at
0

Comments

0 comments
Login to comment

Related

Cannot reorder an array of objects in JS

How can I reorder the objects in a javascript array?

Reorder objects in array based on one of their string properties?

Reorder the objects in an array by object property numeric values

How to "expand" sub array in array of objects

Create or reorder an array based on values from array of objects

Reorder array of objects that contain parent/child relationships with lodash or vanilla js

How can I reorder an ELEMENT inside of an OBJECT of an array of OBJECTS NOT sort?

How to reorder array of Core Data Objects and reorder it's Int attributes in Swift 3

Expand Array property in Object to Multiple Objects populated w/ Array value

How to reorder an array of objects using other array that has index and returns the new ordered array

How to expand subarray in array of objects and delete string some element

Reorder an array with drag and drop

php reorder array of strings

Reorder array in JS

How to reorder an Array

SwiftUI reorder CoreData Objects in List

Reorder array according to reference array

Expand/Collapsible list of objects

PHP merge and reorder multidimensional array

How to reorder this array without an index?

How to reshape numpy array with reorder?

How to reorder places in char array

Reorder elements in array using php

How to transform and reorder array in JavaScript?

Reorder Array based on value in an object

Allow users to reorder multiple different objects together

Reorder objects in a list based on an existing pattern in R

Reorder objects - copy (cut) object into another