How to get a slice from "arguments"

Javascript

Javascript Problem Overview


All you know that arguments is a special object that holds all the arguments passed to the function.

And as long as it is not an array - you cannot use something like arguments.slice(1).

So the question - how to slice everything but first element from arguments?

UPD:

seems like there is no way without converting it to an array with

var args = Array.prototype.slice.call(arguments);

If someone posts another solution it would be great, if not - I'll check the first one with the line above as an answer.

Javascript Solutions


Solution 1 - Javascript

Q. How to slice everything but first element from arguments?

The following will return an array containing all arguments except the first:

var slicedArgs = Array.prototype.slice.call(arguments, 1);

You don't have to convert arguments to an array first, do it all in one step.

Solution 2 - Javascript

Meddling with array functions is not actually necessary.

Using rest parameter syntax ...rest is cleaner and more convenient.

Example

function argumentTest(first, ...rest) {
  console.log("First arg:" + first);

  // loop through the rest of the parameters
  for (let arg of rest) {
    console.log("- " + arg);
  }
}

// call your function with any number of arguments
argumentTest("first arg", "#2", "more arguments", "this is not an argument but a contradiction");

...Rest

Solution 3 - Javascript

From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments:

> You should not slice on arguments because it prevents optimizations in > JavaScript engines (V8 for example). Instead, try constructing a new > array by iterating through the arguments object.

So Paul Rosiana's answer above is correct

Solution 4 - Javascript

This can be a way:

var args = Array.from(arguments).slice(1);

Solution 5 - Javascript

You can "slice without slicing" by procedurally walking the arguments object:

function fun() {
  var args = [];

  for (var i = 1; i < arguments.length; i++) {
    args.push(arguments[i]);
  }

  return args;
}

fun(1, 2, 3, 4, 5); //=> [2, 3, 4, 5]

Solution 6 - Javascript

You can use the method [].slice.call(arguments, 1)

[].slice will return you the slice function object and you can call it as the arguments and 1 are the parameters

Solution 7 - Javascript

You can use ...rest within the function to separate the first and the rest of the arguments:

function foo(arr) {
  const [first, ...rest] = arguments;
  console.log(`first = ${first}`);
  console.log(`rest = ${rest}`);
}
//Then calling the function with 3 arguments:
foo(1,2,3)

Solution 8 - Javascript

you can use this too

function arg(myArr) {
  let arg = Object.values(arguments).slice(2, 4);
  console.log(arg);
  return arg;
};

arg([1, 2, 3], 4, [5,6], 7)

see here for reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/values

...

Solution 9 - Javascript

Arguments type is iterable, so using the ES6 (ES2015) spread ... operator, then use Array.slice([start], [end]) method, such as

function omitFirstAndLastArgument(value) {
  const args = arguments.length > 2 ? [...arguments].slice(1, -1) : [];
  return args;
}

omitFirstAndLastArgument(1, 2, 3, 4, 5, 6); //  [2, 3, 4, 5]

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionzerkmsView Question on Stackoverflow
Solution 1 - JavascriptnnnnnnView Answer on Stackoverflow
Solution 2 - JavascriptUmur KaragözView Answer on Stackoverflow
Solution 3 - JavascriptbluescrubbieView Answer on Stackoverflow
Solution 4 - JavascriptDi TranView Answer on Stackoverflow
Solution 5 - JavascriptPaul RosaniaView Answer on Stackoverflow
Solution 6 - JavascriptThierrykView Answer on Stackoverflow
Solution 7 - JavascriptntarlapanView Answer on Stackoverflow
Solution 8 - JavascriptOpportunisticHypeView Answer on Stackoverflow
Solution 9 - JavascriptAamirRView Answer on Stackoverflow