0

I have a function and I pass it an object and some arguments. EX:

someFunc: function(obj){

     var cra = Array.prototype.call(arguments);

so, I call this function passing the following arguments:

someFunc({name: 'frank', age: '56', Location: 'New Heaven'}, 'name, 'age');

I want to have the new Array created "cra" contain all the arguments except the first argument argument[0] which is an object.

A for loop does not work and I don't want to use loops here. Is there something I am missing?

basically:

console.log(cra): 
>>> ['name','age']
2
  • 2
    You can use shift function to remove the first argument of an Array. Commented Mar 13, 2013 at 19:04
  • 1
    cra.shift() would modify the array to be what you want, cra.slice(1) would return what you want. Commented Mar 13, 2013 at 19:04

1 Answer 1

2

you could use

function argArray(){
    return Array.prototype.splice.call(arguments, 1);
}

usage:

argArray(1,2,3,4); // [2,3,4]

This is like doing [1,2,3,4].splice(1);, with the added bonus that you are casting arguments to an Array object.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.