search

Home  >  Q&A  >  body text

javascript - es6 problem of converting class array into array

Chestnut:
var a = {'1':'gg','2':'love','4':'meimei',length:5};
var arr=[.. .a];
console.log(arr);

Error reported under Google Chrome: Uncaught TypeError: a[Symbol.iterator] is not a function

Definition of array-like:/a/11...

javascript introduction documentation on ‘...’: https://developer.mozilla.org...

The official JavaScript documentation says that '...' can convert a class array into an array. Why does it still report an error? confuse!

扔个三星炸死你扔个三星炸死你2705 days ago719

reply all(3)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-06-30 09:57:43

    You should read the article you posted carefully first.

    The reason why it is called "array-like" is because it is similar to "array". You cannot use array methods directly, but you can use array-like methods just like arrays.

    You cannot use the array method directly. Isn’t your extension statement used directly?

    You need to convert the class array into an array first, you can use Array.prototype.slice.call(arguments)

    introduced in the article

    ES6 also has a method, Array.from(arrayLike)

    reply
    0
  • 習慣沉默

    習慣沉默2017-06-30 09:57:43

    Please use Array.from()

    If you look at the English page, you will find that the expanded object needs to be traversable

    Only for iterables

    Spread syntax can be applied only to iterable objects:

    var obj = {'key1': 'value1'};
    var array = [...obj]; // TypeError: obj is not iterable

    reply
    0
  • 滿天的星座

    滿天的星座2017-06-30 09:57:43

    ES6 has so many new features, do you expect the browser to implement them all?

    reply
    0
  • Cancelreply