4

I found the Spread operator can be used with arguments:

function foo() {
    [...arr] = arguments;
    console.log(arr, Array.isArray(arr));
}

foo(1, 3, 5);

// note that we could also use function foo(...args) { } to begin with

But not with an object that is array-like (it would throw an error TypeError: undefined is not a function):

let obj = { 0: "abc", 1: 3.14, length: 2 };

[...arr] = obj;
console.log(arr, Array.isArray(arr));

So can the Spread operator be used with array-like objects?

As a side note, the following could both make an array:

let obj = { 0: "abc", 1: 3.14, length: 2 };
let arr = Array.prototype.slice.call(obj);
console.log(arr, Array.isArray(arr));

let arr2 = Array.from(obj);
console.log(arr2, Array.isArray(arr2));
nonopolarity
  • 130,775
  • 117
  • 415
  • 675
  • 7
    Spread syntax works on any object that implements the [iterable protocol](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterable_protocol) – Patrick Roberts Dec 16 '19 at 20:12
  • 4
    This is a pedantic comment, but that's what Stack Overflow is all about: the `...` syntax is not an "operator"; spread syntax is not part of the expression grammar. – Pointy Dec 16 '19 at 20:13
  • Seems like it'd be possible to create a function that'd return an Iterable object wrapper around a plain "dumb" object. – Pointy Dec 16 '19 at 20:17
  • As a side note, does this actually have a duplicate target or should I go ahead and write an actual answer? – Patrick Roberts Dec 16 '19 at 20:17
  • You can spread both iterable and non-iterable objects but it means something depending on both the source and the destination. – Aluan Haddad Dec 16 '19 at 20:20
  • I guess `[...arr] = Array.from(obj)` is close enough then? – LukStorms Dec 16 '19 at 20:21
  • 1
    @PatrickRoberts I just did a couple of searches for a dup and failed. Closest I found was https://stackoverflow.com/questions/29886552/why-are-objects-not-iterable-in-javascript/29891072#29891072 which shows how to write a wrapper function. – Barmar Dec 16 '19 at 20:21
  • 1
    @Barmar this seems to be the same as the question in part 3 of that answer, in my opinion at least. – Patrick Roberts Dec 16 '19 at 20:23
  • @LukStorms That's simply superfluous, just do `const arr = Array.from(obj)` – Bergi Dec 16 '19 at 22:23
  • @Bergi I know it's super silly. ;) But `[...arr]` is 1 character less than `const arr` ;p Oh darn, `let arr` is the better golf-code. Darn, "let objects be objects, don't iterate them" my granny always told me. – LukStorms Dec 16 '19 at 22:30
  • @LukStorms I had expected there was a `const`/`let`/`var` in front of `[...arr]` as well. Without it, it's an assignment to an undeclared variable, and would throw an exception in strict mode. The sloppy code in the OP actually creates a global variable `arr`. – Bergi Dec 16 '19 at 22:32

0 Answers0