Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 16000

Javascript, partially reordering array

$
0
0

What is the best way to partially re-order an array?

I have created a very basic example;Basically I want to reorder the array (before) using the (indexes) stored in the array.

Using the code I have outputs: 14,21,10,13.However I would like to then keep the remainder of the array that isn't included added to the new after array in the order they originally appear.

This would leave the after array populated as the following: 14,21,10,13 23,8,15

const before = [23, 21, 14, 12, 10, 8, 15]const indexes = [2,1,4,0];const reorderByIndexes = (arr, order) => order.map((index) => arr[index]);const after = reorderByIndexes(before, indexes);console.log(after.join());// 14,21,10,13 23,8,15

I know on a simplistic example like this I could use a loop to iterate over them but the final version is set to be a lot larger.


Viewing all articles
Browse latest Browse all 16000

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>