swaggyP
swaggyP

Reputation: 365

converting arguments to array

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']

Upvotes: 0

Views: 181

Answers (1)

bevacqua
bevacqua

Reputation: 48476

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.

Upvotes: 2

Related Questions