0

I'm trying to create a function in JavaScript that rearranges the indices of an array. I came up with the following idea, but using this method I have to make a new else if clause for every possible length of the array given as fourth argument for the method reformatArray. Can parameters in a method be added in a more intuitive way?

Code:

function start() {
    var array1 = ["Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 6", "Item 7", "Item 8"];

    reformatArray(array1, 1, 2, [1, 2, 0]);
    //output should be ["Item 1", "Item 3", "Item 4", "Item 2", "Item 4", "Item 5", "Item 6", "Item 7", "Item 8"] 
    //WORKS

    var array2 = ["Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 6", "Item 7", "Item 8"];

    reformatArray(array2, 2, 5, [3, 1, 3, 2]);
    //output should be ["Item 1", "Item 2", "Item 6", "Item 4", "Item 6", "Item 5", "Item 8"] 
    //DOES NOT WORK because array as fourth argument is greater than 3 in length
}

function reformatArray(array, startIndex, numOfIndicesToReplace, newIndicesPositions) {
    var newPosLength = newIndicesPositions.length;

    if (newPosLength == 0) {
        array.splice(startIndex, numOfIndicesToReplace);
    } else if (newPosLength == 1) {
        array.splice(startIndex, numOfIndicesToReplace, array[startIndex + newIndicesPositions[0]]);
    } else if (newPosLength == 2) {
        array.splice(startIndex, numOfIndicesToReplace, array[startIndex + newIndicesPositions[0]], array[startIndex + newIndicesPositions[1]]);
    } else if (newPosLength == 3) {
        array.splice(startIndex, numOfIndicesToReplace, array[startIndex + newIndicesPositions[0]], array[startIndex + newIndicesPositions[1]], array[startIndex + newIndicesPositions[2]]);
    }
    //etc.
}

Thanks in advance.

The Coding Wombat
  • 805
  • 1
  • 10
  • 29
  • 1
    What exactly are trying to achieve? – It-Z May 27 '16 at 20:35
  • That all the values in the `newIndicesPositions` array are added as individual parameters in the `splice` method. – The Coding Wombat May 27 '16 at 20:39
  • In the function `reformatArray`, I added a comment `//etc` what I mean by that is that more if else clauses should be added which check the length of newIndicesPositions and add parameters to the splice method accordingly. – The Coding Wombat May 27 '16 at 20:41

4 Answers4

1

You can create a list of params to pass to splice and use Function.prototype.apply to pass them to splice.

function reformatArray(array, startIndex, numOfIndicesToReplace, newIndicesPositions) {
  var newPosLength = newIndicesPositions.length;
  var params = [startIndex, numOfIndicesToReplace];

  newIndicesPositions.forEach(function(val, pos) {
    params.push(array[startIndex + newIndicesPositions[pos]])
  });

  array.splice.apply(array, params);
}
Steven Lambert
  • 5,571
  • 2
  • 29
  • 46
0

Use apply. First param is what's bound to this and second is an array of arguments.

array.splice.apply(null, arrayOfArguments)

0

Take the parameters as object. This helps extending the count of parameters.

function reformatArray(params) {
    var array = params.array;
    var startIndex = params.startIndex;
    var numOfIndicesToReplace = params.numOfIndicesToReplace;
    var newIndicesPositions = params.newIndicesPositions;

    var newPosLength = newIndicesPositions.length;

    if (newPosLength == 0) {
        array.splice(startIndex, numOfIndicesToReplace);
    } else if (newPosLength == 1) {
    array.splice(startIndex, numOfIndicesToReplace, array[startIndex +      newIndicesPositions[0]]);
    } else if (newPosLength == 2) {
        array.splice(startIndex, numOfIndicesToReplace, array[startIndex + newIndicesPositions[0]], array[startIndex + newIndicesPositions[1]]);
    } else if (newPosLength == 3) {
        array.splice(startIndex, numOfIndicesToReplace, array[startIndex + newIndicesPositions[0]], array[startIndex + newIndicesPositions[1]], array[startIndex + newIndicesPositions[2]]);
    }
     //etc.
 }

Or you can use the Arguments object inside method. Arguments is a keyword. arguments object is an Array-like object corresponding to the arguments passed to a function. For more info on Arguments keyword syntax check the MDN website.

Aditya
  • 861
  • 5
  • 8
0

Here's another way you could do it by splicing in the array itself and then concatenating the arrays

function reformatArray2(array, startIndex, numOfIndicesToReplace, newIndicesPositions) {        
    var replaceValues = newIndicesPositions.map(function(index) {
      return array[startIndex + index];
    })

    array.splice(startIndex, numOfIndicesToReplace, replaceValues);
    return [].concat.apply([], array);
}

Plunker Demo

(Array flattening technique found here)

Community
  • 1
  • 1
Shaun
  • 2,012
  • 20
  • 44