How to Convert the "Arguments" Object to an Array in JavaScript

How can I convert the arguments object to an array in JavaScript?

ES6 using rest parameters

If you are able to use ES6 you can use:

Rest Parameters

function sortArgs(...args) {  return args.sort(function (a, b) { return a - b; });}
document.body.innerHTML = sortArgs(12, 4, 6, 8).toString();

Why isn't a function's arguments object an array in Javascript?

My conjecture:

The concept of the arguments object has been on the language since the very beginning, it's even described in the ECMAScript First Edition Standard(PDF).

In that version of ECMAScript, the Array.prototype was really basic, array objects contained only 4 methods!: toString, join, reverse and sort.

I think that's one of the major reasons about they make arguments to inherit from Object.prototype, at that time those Array methods didn't look too useful.

But the Array.prototype object was extended in the next versions of the standard, now on ES5, Array objects have methods such as map, reduce, every, some, etc, that are really powerful.

The last year, there was a proposal in ES5 to make arguments inherit from Array.prototype, in the draft stages of the standard, but was dropped off time later.

In those drafts, arguments inherited from Array.prototype, but for backwards compatibility with ES3, the arguments object had defined two own properties, toString and toLocaleString, both pointing to the same methods on Object.prototype, but finally, the committee decided to keep inheriting from Object.prototype.

how to convert arguments into flat array

Slice only the first argument?

function test() {
console.log(Array.prototype.slice.call(arguments[0]));
}

test([1,2]);

Or loop through the arguments flattening the array:

function test() {
var args = [],
i = 0;
for ( ; i < arguments.length; ++i )
{
if ( Array.isArray( arguments[i] ) )
Array.prototype.push.apply( args, arguments[i] );
else
args.push( arguments[i] );
}
console.log( args );
}

test([1,2], [3,4,5], 6, [7], 8, 9);

JSFIDDLE

Why can't I convert the argument object to an array with concat method in JavaScript?

The details can be found in §15.4.4.4 of the spec. Basically, it starts out by creating a list of things to process, where the first item on the list is the the object you called it on (this) and then subsequent items on the list are the arguments to the function. Then it loops through the list and processes each item. It has an explicit branch in it for items on that list that aren't arrays, and the "else" of that branch is (Step 5(c)):

  • Else, E is not an Array

    • Call the [[DefineOwnProperty]] internal method of A with arguments ToString(n), Property Descriptor {[[Value]]: E, [[Writable]]: true, [[Enumerable]]: true, [[Configurable]]: true}, and false.
    • Increase n by 1.

So you can call concat on a non-array. It doesn't fail. But it doesn't treat non-array array-like things as though they were arrays.

Note Felix's observation that this "only arrays get their entries spread out" is changing in ES6. concat uses a new "is concat spreadable" flag on objects to determine whether it should loop through their entries. I don't think the arguments object will have that flag, though (at least, I don't see anything saying so in §9.4.4), probably to avoid issues with backward-compatibility.

Convert Array to Object

ECMAScript 6 introduces the easily polyfillable Object.assign:

The Object.assign() method is used to copy the values of all
enumerable own properties from one or more source objects to a target
object. It will return the target object.

Object.assign({}, ['a','b','c']); // {0:"a", 1:"b", 2:"c"}

The own length property of the array is not copied because it isn't enumerable.

Also, you can use ES8 spread syntax on objects to achieve the same result:

{ ...['a', 'b', 'c'] }

For custom keys you can use reduce:

['a', 'b', 'c'].reduce((a, v) => ({ ...a, [v]: v}), {}) 
// { a: "a", b: "b", c: "c" }

Obtain arguments from a string seperated by a space and convert an argument in an array format to an array

Let's assume no funny characters as the input. Also nesting not allowed.

var str = "arg1 [ elem1  , elem2,elem3  ]  arg3";
console.log(str)

// removing white spaces from the [ array ]
str = str.replace(/\s*,\s*/g, ',');
str = str.replace(/\[\s*/g, '[');
str = str.replace(/\s*\]/g, ']');

// now split on words
var arr = str.split(/\s+/);
arr = arr.map(function(elem) {

// if begins with [ it is assumed to be an array to be splitted
return elem.charAt(0) == '[' ? elem.slice(1, -1).split(",") : elem;
})

console.log(arr)

Convert arguments to object?

A function is superfluous for this. The functionality you are looking for is already part of the ES6 syntax as a shorthand notation for object initialisation. You just need to write { a, b, c } instead of uselessFunction(a, b, c).

const foo = function (a, b, c) {  const args = { a, b, c };  console.log(args);};
foo(2, 3, 4);


Related Topics



Leave a reply



Submit