I am making a board game using react. I am using an array of objects in the form
[
{ id : 1, tile : number, name : string},
{ id : 2, tile : number, name : string},
{ id : 3, tile : number, name : string},
{ id : 4, tile : number, name : string},
{ id : 5, tile : number, name : string},
{ id : 6, tile : number, name : string},
{ id : 7, tile : number, name : string},
{ id : 8, tile : number, name : string},
{ id : 9, tile : number, name : string},
{ id : 10, tile : number, name : string},
{ id : 11, tile : number, name : string},
.
.
.
{ id : 100, tile : number, name : string},
]
I want to sort the id 1 -10 as ascending, 11 - 20 as descending, 21 - 30 as ascending 31 to 40 as desc, and so on. i have tried multiple methods
splitting the entire array (having 100 objects) into groups of 10 and then applying sort (asc/desc) to them and finally using the concat() to get the final sorted array
using splice(0, 10).sort((a, b) => a.id > b.id ? 1 : -1).map(value => value.id); //for first 10 ids spice(11, 20).sort((a, b) => a.id > b.id ? 1 : -1).reverse().map(value => value.id); //for 11 - 20 ids and following the same pattern till 91 - 100.
putting conditions inside the sort itself (based on the units and tens place digits of the individual ids..) for eg :
const Tiles = [ {}, {}, {}, ..... {}, //100 objects with unique ids ] const sortTiles = Tiles.sort((a : any, b : any) => parseInt(String(a.id).charAt(0) % 2 == 0)? 1 : -1); //for sorting 20s, 40s, 60s, 80s digits as ascending...
but i want an optimal way of sorting the numbers as per the scheme I mentioned above.
1 - 10 asc - left to right
11 - 20 desc - left to right
21 - 30 asc - left to right
31 - 40 desc - left to right
... and so on.
here is my code - please suggest better solutions
const Tiles = [
{
id: 1,
tilenumber: 1,
name: "start"
},
{
id: 2,
tilenumber: 2,
name: "two"
},
{
id: 3,
tilenumber: 3,
name: "three"
},
{
id: 4,
tilenumber: 4,
name: "four"
},
{
id: 5,
tilenumber: 5,
name: "five"
},
{
id: 6,
tilenumber: 6,
name: "six"
},
{
id: 7,
tilenumber: 7,
name: "seven"
},
{
id: 8,
tilenumber: 8,
name: "eight"
},
{
id: 9,
tilenumber: 9,
name: "nine"
},
{
id: 10,
tilenumber: 10,
name: "ten"
},
{
id: 11,
tilenumber: 11,
name: "eleven"
},
{
id: 12,
tilenumber: 12,
name: "tweleve"
},
{
id: 13,
tilenumber: 13,
name: "thirteen"
},
{
id: 14,
tilenumber: 14,
name: "fourten"
},
{
id: 15,
tilenumber: 15,
name: "fifteen"
},
{
id: 16,
tilenumber: 16,
name: "sixteen"
},
{
id: 17,
tilenumber: 17,
name: "seventeen"
},
{
id: 18,
tilenumber: 18,
name: "eighteen"
},
{
id: 19,
tilenumber: 19,
name: "nineteen"
},
{
id: 20,
tilenumber: 20,
name: "twenty"
},
{
id: 21,
tilenumber: 21,
name: "twentyone"
},
{
id: 22,
tilenumber: 22,
name: "twentytwo"
},
{
id: 23,
tilenumber: 23,
name: "twentythree"
},
{
id: 24,
tilenumber: 24,
name: "twentyfour"
},
{
id: 25,
tilenumber: 25,
name: "twentyfive"
},
{
id: 26,
tilenumber: 26,
name: "twentysix"
},
{
id: 27,
tilenumber: 27,
name: "twentyseven"
},
{
id: 28,
tilenumber: 28,
name: "twentyeight"
},
{
id: 29,
tilenumber: 29,
name: "twentynine"
},
{
id: 30,
tilenumber: 30,
name: "thirty"
},
{
id: 31,
tilenumber: 31,
name: "thirtyone"
},
{
id: 32,
tilenumber: 32,
name: "thirtytwo"
},
{
id: 33,
tilenumber: 33,
name: "thirtythree"
},
{
id: 34,
tilenumber: 34,
name: "thirtyfour"
},
{
id: 35,
tilenumber: 35,
name: "thirtyfive"
},
{
id: 36,
tilenumber: 36,
name: "thirtysix"
},
{
id: 37,
tilenumber: 37,
name: "thirtyseven"
},
{
id: 38,
tilenumber: 38,
name: "thirtyeight"
},
{
id: 39,
tilenumber: 39,
name: "thirtynine"
},
{
id: 40,
tilenumber: 40,
name: "fourty"
},
{
id: 41,
tilenumber: 41,
name: "fourtyOne"
},
{
id: 42,
tilenumber: 42,
name: "fourtyTwo"
},
{
id: 43,
tilenumber: 43,
name: "fourtyThree"
},
{
id: 44,
tilenumber: 44,
name: "fourtyFour"
},
{
id: 45,
tilenumber: 45,
name: "fourtyFive"
},
{
id: 46,
tilenumber: 46,
name: "fourtySix"
},
{
id: 47,
tilenumber: 47,
name: "fourtySeven"
},
{
id: 48,
tilenumber: 48,
name: "fourtyEight"
},
{
id: 49,
tilenumber: 49,
name: "fourtyNine"
},
{
id: 50,
tilenumber: 50,
name: "fifty"
},
{
id: 51,
tilenumber: 51,
name: "fiftyOne"
},
{
id: 52,
tilenumber: 52,
name: "fiftyTwo"
},
{
id: 53,
tilenumber: 53,
name: "fiftyThree"
},
{
id: 54,
tilenumber: 54,
name: "fiftyFour"
},
{
id: 55,
tilenumber: 55,
name: "fiftyFive"
},
{
id: 56,
tilenumber: 56,
name: "fiftySix"
},
{
id: 57,
tilenumber: 57,
name: "fiftySeven"
},
{
id: 58,
tilenumber: 58,
name: "fiftyEight"
},
{
id: 59,
tilenumber: 59,
name: "fiftyNine"
},
{
id: 60,
tilenumber: 60,
name: "Sixty"
},
{
id: 61,
tilenumber: 61,
name: "sixtyOne"
},
{
id: 62,
tilenumber: 62,
name: "sixtyTwo"
},
{
id: 63,
tilenumber: 63,
name: "sixtyThree"
},
{
id: 64,
tilenumber: 64,
name: "sixtyFour"
},
{
id: 65,
tilenumber: 65,
name: "sixtyFive"
},
{
id: 66,
tilenumber: 66,
name: "sixtySix"
},
{
id: 67,
tilenumber: 67,
name: "sixtySeven"
},
{
id: 68,
tilenumber: 68,
name: "sixtyEight"
},
{
id: 69,
tilenumber: 69,
name: "sixtyNine"
},
{
id: 70,
tilenumber: 70,
name: "Seventy"
}, {
id: 71,
tilenumber: 71,
name: "seventyOne"
},
{
id: 72,
tilenumber: 72,
name: "seventyTwo"
},
{
id: 73,
tilenumber: 73,
name: "seventyThree"
},
{
id: 74,
tilenumber: 74,
name: "seventyFour"
},
{
id: 75,
tilenumber: 75,
name: "seventyFive"
},
{
id: 76,
tilenumber: 76,
name: "seventySix"
},
{
id: 77,
tilenumber: 77,
name: "seventySeven"
},
{
id: 78,
tilenumber: 78,
name: "seventyEight"
},
{
id: 79,
tilenumber: 79,
name: "seventyNine"
},
{
id: 80,
tilenumber: 80,
name: "Eighty"
}, {
id: 81,
tilenumber: 81,
name: "eightyOne"
},
{
id: 82,
tilenumber: 82,
name: "eightyTwo"
},
{
id: 83,
tilenumber: 83,
name: "eightyThree"
},
{
id: 84,
tilenumber: 84,
name: "eightyFour"
},
{
id: 85,
tilenumber: 85,
name: "eightyFive"
},
{
id: 86,
tilenumber: 86,
name: "eightSix"
},
{
id: 87,
tilenumber: 87,
name: "eightySeven"
},
{
id: 88,
tilenumber: 88,
name: "eightyEight"
},
{
id: 89,
tilenumber: 89,
name: "eightyNine"
},
{
id: 90,
tilenumber: 90,
name: "Ninety"
}, {
id: 91,
tilenumber: 91,
name: "ninetyOne"
},
{
id: 92,
tilenumber: 92,
name: "ninetyTwo"
},
{
id: 93,
tilenumber: 93,
name: "ninetyThree"
},
{
id: 94,
tilenumber: 94,
name: "ninetyFour"
},
{
id: 95,
tilenumber: 95,
name: "ninetyFive"
},
{
id: 96,
tilenumber: 96,
name: "ninetySix"
},
{
id: 97,
tilenumber: 97,
name: "ninetySeven"
},
{
id: 98,
tilenumber: 98,
name: "ninetyEight"
},
{
id: 99,
tilenumber: 99,
name: "ninetyNine"
},
{
id: 100,
tilenumber: 100,
name: "Hundread"
}
];
/*var newSetTiles = Tiles.sort((a : any , b : any) =>
parseInt(String(a.id).charAt(0))%2 == 0 || parseInt(String(a.id).charAt(1)) == 0 ? 1 : -1);
console.log(newSetTiles, 'sorted'); */
const tileSetA = Tiles.slice(0, 10).sort((a : any, b : any) => a.id > b.id ? 1 : -1);
const tileSetB = Tiles.slice(11, 20).sort((a : any, b : any) => a.id > b.id ? 1 : -1).reverse();
const tileSetC = Tiles.slice(21, 30).sort((a : any, b : any) => a.id > b.id ? 1 : -1);
const concatSet = tileSetA.concat(tileSetB);
const concatSet_ = concatSet.concat(tileSetB);
console.log(concatSet_);
export default concatSet_;
CodePudding user response:
How about this:
Create a loop for each slice, if even index just append it to new array, if odd reverse order then append it to new array.
const Tiles = [{
id: 1,
tilenumber: 1,
name: "start"
},
{
id: 2,
tilenumber: 2,
name: "two"
},
{
id: 3,
tilenumber: 3,
name: "three"
},
{
id: 4,
tilenumber: 4,
name: "four"
},
{
id: 5,
tilenumber: 5,
name: "five"
},
{
id: 6,
tilenumber: 6,
name: "six"
},
{
id: 7,
tilenumber: 7,
name: "seven"
},
{
id: 8,
tilenumber: 8,
name: "eight"
},
{
id: 9,
tilenumber: 9,
name: "nine"
},
{
id: 10,
tilenumber: 10,
name: "ten"
},
{
id: 11,
tilenumber: 11,
name: "eleven"
},
{
id: 12,
tilenumber: 12,
name: "tweleve"
},
{
id: 13,
tilenumber: 13,
name: "thirteen"
},
{
id: 14,
tilenumber: 14,
name: "fourten"
},
{
id: 15,
tilenumber: 15,
name: "fifteen"
},
{
id: 16,
tilenumber: 16,
name: "sixteen"
},
{
id: 17,
tilenumber: 17,
name: "seventeen"
},
{
id: 18,
tilenumber: 18,
name: "eighteen"
},
{
id: 19,
tilenumber: 19,
name: "nineteen"
},
{
id: 20,
tilenumber: 20,
name: "twenty"
},
{
id: 21,
tilenumber: 21,
name: "twentyone"
},
{
id: 22,
tilenumber: 22,
name: "twentytwo"
},
{
id: 23,
tilenumber: 23,
name: "twentythree"
},
{
id: 24,
tilenumber: 24,
name: "twentyfour"
},
{
id: 25,
tilenumber: 25,
name: "twentyfive"
},
{
id: 26,
tilenumber: 26,
name: "twentysix"
},
{
id: 27,
tilenumber: 27,
name: "twentyseven"
},
{
id: 28,
tilenumber: 28,
name: "twentyeight"
},
{
id: 29,
tilenumber: 29,
name: "twentynine"
},
{
id: 30,
tilenumber: 30,
name: "thirty"
},
{
id: 31,
tilenumber: 31,
name: "thirtyone"
},
{
id: 32,
tilenumber: 32,
name: "thirtytwo"
},
{
id: 33,
tilenumber: 33,
name: "thirtythree"
},
{
id: 34,
tilenumber: 34,
name: "thirtyfour"
},
{
id: 35,
tilenumber: 35,
name: "thirtyfive"
},
{
id: 36,
tilenumber: 36,
name: "thirtysix"
},
{
id: 37,
tilenumber: 37,
name: "thirtyseven"
},
{
id: 38,
tilenumber: 38,
name: "thirtyeight"
},
{
id: 39,
tilenumber: 39,
name: "thirtynine"
},
{
id: 40,
tilenumber: 40,
name: "fourty"
},
{
id: 41,
tilenumber: 41,
name: "fourtyOne"
},
{
id: 42,
tilenumber: 42,
name: "fourtyTwo"
},
{
id: 43,
tilenumber: 43,
name: "fourtyThree"
},
{
id: 44,
tilenumber: 44,
name: "fourtyFour"
},
{
id: 45,
tilenumber: 45,
name: "fourtyFive"
},
{
id: 46,
tilenumber: 46,
name: "fourtySix"
},
{
id: 47,
tilenumber: 47,
name: "fourtySeven"
},
{
id: 48,
tilenumber: 48,
name: "fourtyEight"
},
{
id: 49,
tilenumber: 49,
name: "fourtyNine"
},
{
id: 50,
tilenumber: 50,
name: "fifty"
},
{
id: 51,
tilenumber: 51,
name: "fiftyOne"
},
{
id: 52,
tilenumber: 52,
name: "fiftyTwo"
},
{
id: 53,
tilenumber: 53,
name: "fiftyThree"
},
{
id: 54,
tilenumber: 54,
name: "fiftyFour"
},
{
id: 55,
tilenumber: 55,
name: "fiftyFive"
},
{
id: 56,
tilenumber: 56,
name: "fiftySix"
},
{
id: 57,
tilenumber: 57,
name: "fiftySeven"
},
{
id: 58,
tilenumber: 58,
name: "fiftyEight"
},
{
id: 59,
tilenumber: 59,
name: "fiftyNine"
},
{
id: 60,
tilenumber: 60,
name: "Sixty"
},
{
id: 61,
tilenumber: 61,
name: "sixtyOne"
},
{
id: 62,
tilenumber: 62,
name: "sixtyTwo"
},
{
id: 63,
tilenumber: 63,
name: "sixtyThree"
},
{
id: 64,
tilenumber: 64,
name: "sixtyFour"
},
{
id: 65,
tilenumber: 65,
name: "sixtyFive"
},
{
id: 66,
tilenumber: 66,
name: "sixtySix"
},
{
id: 67,
tilenumber: 67,
name: "sixtySeven"
},
{
id: 68,
tilenumber: 68,
name: "sixtyEight"
},
{
id: 69,
tilenumber: 69,
name: "sixtyNine"
},
{
id: 70,
tilenumber: 70,
name: "Seventy"
}, {
id: 71,
tilenumber: 71,
name: "seventyOne"
},
{
id: 72,
tilenumber: 72,
name: "seventyTwo"
},
{
id: 73,
tilenumber: 73,
name: "seventyThree"
},
{
id: 74,
tilenumber: 74,
name: "seventyFour"
},
{
id: 75,
tilenumber: 75,
name: "seventyFive"
},
{
id: 76,
tilenumber: 76,
name: "seventySix"
},
{
id: 77,
tilenumber: 77,
name: "seventySeven"
},
{
id: 78,
tilenumber: 78,
name: "seventyEight"
},
{
id: 79,
tilenumber: 79,
name: "seventyNine"
},
{
id: 80,
tilenumber: 80,
name: "Eighty"
}, {
id: 81,
tilenumber: 81,
name: "eightyOne"
},
{
id: 82,
tilenumber: 82,
name: "eightyTwo"
},
{
id: 83,
tilenumber: 83,
name: "eightyThree"
},
{
id: 84,
tilenumber: 84,
name: "eightyFour"
},
{
id: 85,
tilenumber: 85,
name: "eightyFive"
},
{
id: 86,
tilenumber: 86,
name: "eightSix"
},
{
id: 87,
tilenumber: 87,
name: "eightySeven"
},
{
id: 88,
tilenumber: 88,
name: "eightyEight"
},
{
id: 89,
tilenumber: 89,
name: "eightyNine"
},
{
id: 90,
tilenumber: 90,
name: "Ninety"
}, {
id: 91,
tilenumber: 91,
name: "ninetyOne"
},
{
id: 92,
tilenumber: 92,
name: "ninetyTwo"
},
{
id: 93,
tilenumber: 93,
name: "ninetyThree"
},
{
id: 94,
tilenumber: 94,
name: "ninetyFour"
},
{
id: 95,
tilenumber: 95,
name: "ninetyFive"
},
{
id: 96,
tilenumber: 96,
name: "ninetySix"
},
{
id: 97,
tilenumber: 97,
name: "ninetySeven"
},
{
id: 98,
tilenumber: 98,
name: "ninetyEight"
},
{
id: 99,
tilenumber: 99,
name: "ninetyNine"
},
{
id: 100,
tilenumber: 100,
name: "Hundread"
}
];
// First make sure they are in order, this may not be needed if you know they start in order
Tiles.sort((a, b) => a.id - b.id);
let newArray = [];
const sections = Math.ceil(Tiles.length / 10)
for (let i = 0; i < sections; i ) {
const slice = Tiles.slice(i * 10, i * 10 10);
if (i % 2 === 1) {
slice.sort((a, b) => b.id - a.id);
}
newArray = newArray.concat(slice);
}
console.log(newArray);
CodePudding user response:
Or if you prefer here is a one-line solution:
const newArray = Array.from({length: Math.ceil(Tiles.length / 10)}, () => Tiles.splice(0, 10)).map((chunk, i) => i % 2 === 0 ? chunk : chunk.sort((a, b) => b.id - a.id)).flat()
See methods:
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice
const Tiles = [{
id: 1,
tilenumber: 1,
name: "start"
},
{
id: 2,
tilenumber: 2,
name: "two"
},
{
id: 3,
tilenumber: 3,
name: "three"
},
{
id: 4,
tilenumber: 4,
name: "four"
},
{
id: 5,
tilenumber: 5,
name: "five"
},
{
id: 6,
tilenumber: 6,
name: "six"
},
{
id: 7,
tilenumber: 7,
name: "seven"
},
{
id: 8,
tilenumber: 8,
name: "eight"
},
{
id: 9,
tilenumber: 9,
name: "nine"
},
{
id: 10,
tilenumber: 10,
name: "ten"
},
{
id: 11,
tilenumber: 11,
name: "eleven"
},
{
id: 12,
tilenumber: 12,
name: "tweleve"
},
{
id: 13,
tilenumber: 13,
name: "thirteen"
},
{
id: 14,
tilenumber: 14,
name: "fourten"
},
{
id: 15,
tilenumber: 15,
name: "fifteen"
},
{
id: 16,
tilenumber: 16,
name: "sixteen"
},
{
id: 17,
tilenumber: 17,
name: "seventeen"
},
{
id: 18,
tilenumber: 18,
name: "eighteen"
},
{
id: 19,
tilenumber: 19,
name: "nineteen"
},
{
id: 20,
tilenumber: 20,
name: "twenty"
},
{
id: 21,
tilenumber: 21,
name: "twentyone"
},
{
id: 22,
tilenumber: 22,
name: "twentytwo"
},
{
id: 23,
tilenumber: 23,
name: "twentythree"
},
{
id: 24,
tilenumber: 24,
name: "twentyfour"
},
{
id: 25,
tilenumber: 25,
name: "twentyfive"
},
{
id: 26,
tilenumber: 26,
name: "twentysix"
},
{
id: 27,
tilenumber: 27,
name: "twentyseven"
},
{
id: 28,
tilenumber: 28,
name: "twentyeight"
},
{
id: 29,
tilenumber: 29,
name: "twentynine"
},
{
id: 30,
tilenumber: 30,
name: "thirty"
},
{
id: 31,
tilenumber: 31,
name: "thirtyone"
},
{
id: 32,
tilenumber: 32,
name: "thirtytwo"
},
{
id: 33,
tilenumber: 33,
name: "thirtythree"
},
{
id: 34,
tilenumber: 34,
name: "thirtyfour"
},
{
id: 35,
tilenumber: 35,
name: "thirtyfive"
},
{
id: 36,
tilenumber: 36,
name: "thirtysix"
},
{
id: 37,
tilenumber: 37,
name: "thirtyseven"
},
{
id: 38,
tilenumber: 38,
name: "thirtyeight"
},
{
id: 39,
tilenumber: 39,
name: "thirtynine"
},
{
id: 40,
tilenumber: 40,
name: "fourty"
},
{
id: 41,
tilenumber: 41,
name: "fourtyOne"
},
{
id: 42,
tilenumber: 42,
name: "fourtyTwo"
},
{
id: 43,
tilenumber: 43,
name: "fourtyThree"
},
{
id: 44,
tilenumber: 44,
name: "fourtyFour"
},
{
id: 45,
tilenumber: 45,
name: "fourtyFive"
},
{
id: 46,
tilenumber: 46,
name: "fourtySix"
},
{
id: 47,
tilenumber: 47,
name: "fourtySeven"
},
{
id: 48,
tilenumber: 48,
name: "fourtyEight"
},
{
id: 49,
tilenumber: 49,
name: "fourtyNine"
},
{
id: 50,
tilenumber: 50,
name: "fifty"
},
{
id: 51,
tilenumber: 51,
name: "fiftyOne"
},
{
id: 52,
tilenumber: 52,
name: "fiftyTwo"
},
{
id: 53,
tilenumber: 53,
name: "fiftyThree"
},
{
id: 54,
tilenumber: 54,
name: "fiftyFour"
},
{
id: 55,
tilenumber: 55,
name: "fiftyFive"
},
{
id: 56,
tilenumber: 56,
name: "fiftySix"
},
{
id: 57,
tilenumber: 57,
name: "fiftySeven"
},
{
id: 58,
tilenumber: 58,
name: "fiftyEight"
},
{
id: 59,
tilenumber: 59,
name: "fiftyNine"
},
{
id: 60,
tilenumber: 60,
name: "Sixty"
},
{
id: 61,
tilenumber: 61,
name: "sixtyOne"
},
{
id: 62,
tilenumber: 62,
name: "sixtyTwo"
},
{
id: 63,
tilenumber: 63,
name: "sixtyThree"
},
{
id: 64,
tilenumber: 64,
name: "sixtyFour"
},
{
id: 65,
tilenumber: 65,
name: "sixtyFive"
},
{
id: 66,
tilenumber: 66,
name: "sixtySix"
},
{
id: 67,
tilenumber: 67,
name: "sixtySeven"
},
{
id: 68,
tilenumber: 68,
name: "sixtyEight"
},
{
id: 69,
tilenumber: 69,
name: "sixtyNine"
},
{
id: 70,
tilenumber: 70,
name: "Seventy"
}, {
id: 71,
tilenumber: 71,
name: "seventyOne"
},
{
id: 72,
tilenumber: 72,
name: "seventyTwo"
},
{
id: 73,
tilenumber: 73,
name: "seventyThree"
},
{
id: 74,
tilenumber: 74,
name: "seventyFour"
},
{
id: 75,
tilenumber: 75,
name: "seventyFive"
},
{
id: 76,
tilenumber: 76,
name: "seventySix"
},
{
id: 77,
tilenumber: 77,
name: "seventySeven"
},
{
id: 78,
tilenumber: 78,
name: "seventyEight"
},
{
id: 79,
tilenumber: 79,
name: "seventyNine"
},
{
id: 80,
tilenumber: 80,
name: "Eighty"
}, {
id: 81,
tilenumber: 81,
name: "eightyOne"
},
{
id: 82,
tilenumber: 82,
name: "eightyTwo"
},
{
id: 83,
tilenumber: 83,
name: "eightyThree"
},
{
id: 84,
tilenumber: 84,
name: "eightyFour"
},
{
id: 85,
tilenumber: 85,
name: "eightyFive"
},
{
id: 86,
tilenumber: 86,
name: "eightSix"
},
{
id: 87,
tilenumber: 87,
name: "eightySeven"
},
{
id: 88,
tilenumber: 88,
name: "eightyEight"
},
{
id: 89,
tilenumber: 89,
name: "eightyNine"
},
{
id: 90,
tilenumber: 90,
name: "Ninety"
}, {
id: 91,
tilenumber: 91,
name: "ninetyOne"
},
{
id: 92,
tilenumber: 92,
name: "ninetyTwo"
},
{
id: 93,
tilenumber: 93,
name: "ninetyThree"
},
{
id: 94,
tilenumber: 94,
name: "ninetyFour"
},
{
id: 95,
tilenumber: 95,
name: "ninetyFive"
},
{
id: 96,
tilenumber: 96,
name: "ninetySix"
},
{
id: 97,
tilenumber: 97,
name: "ninetySeven"
},
{
id: 98,
tilenumber: 98,
name: "ninetyEight"
},
{
id: 99,
tilenumber: 99,
name: "ninetyNine"
},
{
id: 100,
tilenumber: 100,
name: "Hundread"
}
];
// First make sure they are in order, this may not be needed if you know they start in order
Tiles.sort((a, b) => a.id - b.id);
const newArray = Array.from({
length: Math.ceil(Tiles.length / 10)
}, () => Tiles.splice(0, 10)).map((chunk, i) => i % 2 === 0 ? chunk : chunk.sort((a, b) => b.id - a.id)).flat()
console.log(newArray);