Haris Muzaffar
Haris Muzaffar

Reputation: 441

How to reorder multiple elements in an array?

I have an array arr:

arr = [0,1,2,3,4,5]

How can I reorder/ shift multiple elements? For example,

In the above example, index 4 is with the perspective of the original array, not the final array.

I tried using splice like this:

items = [0,2,3] // items to be reordered
indexes = [0,2,3] // get their indexes
arr = [...arr.filter((it)=> !indexes.includes(arr.indexOf(it)))]
arr.splice(4, 0, ...items)
// result [1, 4, 5, 0, 2, 3]

The above is not the intended result

Upvotes: 1

Views: 944

Answers (4)

Andy
Andy

Reputation: 63550

const temp = [0, 2, 3];
const arr = [0, 1, 2, 3, 4, 5];
const index = arr[4];

// Accepts an array (temp) and returns a function to be used
// as the callback for `filter` which accepts an element
// and returns whether that element is in the temp array
const filterUsing = (arr) => (el) => !arr.includes(el);

// `filter` the elements from the main array
const filtered = arr.filter(filterUsing(temp));

// Find the new position of the element in `index`
const newIndex = filtered.findIndex(el => el === index);

// Splice in the temp array back into the filtered array
filtered.splice(newIndex, 0, ...temp);

console.log(filtered);

Upvotes: 1

Nina Scholz
Nina Scholz

Reputation: 386680

This solution mutates the array.

You could store the value at the inser position and remove the items and splice the removed items after the index of the stored item.

position             v
index    0  1  2  3  4
array   [0, 1, 2, 3, 4, 5]              store value at index
            1        4  5               rest array after removing items
            1        4 [0  2  3] 5      splice with removed items

var array = [0, 1, 2, 3, 4, 5],
    remove = [0, 2, 3],
    insertAtIndex = 4,
    valueAtPosition = array[insertAtIndex];
    
remove.forEach(v => array.splice(array.indexOf(v), 1));
array.splice(array.indexOf(valueAtPosition) + 1, 0, ...remove);

console.log(...array);

Upvotes: 2

StepUp
StepUp

Reputation: 38164

Try to use this approach:

let arr = [0,1,2,3,4,5];
let rmv = [0, 2, 3];

const remove = (src, rem, i ) => {
  const arrWithIndexes = src.map((a, i) => { return  {value: a, index: i}});
  const filtered = arrWithIndexes.filter(f => !rem.some(s=> s === f.value));
  const indexToInsert = filtered.findIndex(f=>f.index === i);
  const result = filtered.map(f=> f.value);
  result.splice(indexToInsert, 0, ...rem);
  console.log(result);
}

console.log(remove(arr, rmv, 4));

Or if you know the desired index:

let arr = [0,1,2,3,4,5];
let rmv = [0, 2, 3];

const remove = (src, rem ) => {
  const filtered = src.filter(f=> !rem.some(s=> s === f));
  filtered.splice(2, 0, ...rmv)
  console.log(filtered);

}

console.log(remove(arr, rmv));

Upvotes: 1

Maheer Ali
Maheer Ali

Reputation: 36584

You can first remove the given elements and then use splice() to add them at the required index.

function shiftElementsTo(arr, inds, final){
   let set = new Set(inds);
   let removed = inds.map(x => arr[x]);
   arr = arr.filter((x, i) => !set.has(i));
   arr.splice(final, 0, ...removed);
   return arr;
}

console.log(shiftElementsTo([0, 1, 2, 3, 4, 5], [0, 2, 3], 2))

Upvotes: 1

Related Questions