• Skip to main content
  • Skip to search
  • Skip to select language
HTML

Structure of content on the web

  • Web APIs

    Interfaces for building web applications

  • Learn
    • CSS

      Learn to style content using CSS

    • Overview

      A customized MDN experience

    • FAQ

      Frequently asked questions about MDN Plus

  • HTTP Observatory

    Scan a website for free

  • JavaScript
  • from()
      • Deutsch
      • 日本語
      • 中文 (简体)

    In this article

    • Syntax
    • Description
    • Examples
    • Specifications
    • Browser compatibility
    • See also
    1. from()
  • Instance methods
    1. find()
    2. reduce()
    3. [Symbol.iterator]()
  • Inheritance
  • call()
  • displayName Non-standard
  • arguments Non-standard Deprecated
  • caller Non-standard Deprecated
  • Instance methods
    1. __defineGetter__() Deprecated
    2. __defineSetter__() Deprecated
    3. __lookupGetter__() Deprecated
    4. __lookupSetter__() Deprecated
    5. toLocaleString()
    6. __proto__ Deprecated
    7. Iterator object from an iterator or iterable object.

  • Syntax

    js
    from(object)
    

    Parameters

    object

    An object that implements the iterator protocol.

    Return value

    If object is an iterable, its [Symbol.iterator]() method is called to obtain the iterator. Otherwise, object is assumed to be an iterator. If the iterator is already proper iterators. All iterator objects returned by Iterator.from() inherit from a common prototype object, which has the following methods:

    next()

    Calls the underlying iterator's next() method and returns the result.

    return()

    Calls the underlying iterator's return() method and returns the result, or returns { value: undefined, done: true } if the underlying iterator doesn't have a return() method.

    Examples

    Converting an iterable to a proper iterator

    Because obj is already an iterable that returns a proper iterator when its [Symbol.iterator]() method is called, Iterator.from(obj) returns the same iterator.

    js
    const iterator = (function* () {
      yield 1;
      yield 2;
      yield 3;
    })();
    
    const obj = {
      [Symbol.iterator]() {
        return iterator;
      },
    };
    
    const iterator2 = Iterator.from(obj);
    console.log(iterator2 === iterator); / true
    

    Because obj2 is an iterable that returns a non-proper iterator when its [Symbol.iterator]() method is called, Iterator.from(obj2) returns a new iterator that wraps the original iterator.

    js
    const iterator = {
      current: 0,
      next() {
        return { value: this.current++, done: false };
      },
    };
    
    const obj2 = {
      [Symbol.iterator]() {
        return iterator;
      },
    };
    
    const iterator2 = Iterator.from(obj2);
    console.log(iterator2 === iterator); / false
    console.log(iterator2.next()); / { value: 0, done: false }
    console.log(iterator.next()); / { value: 1, done: false }
    

    Converting an iterator to a proper iterator

    Because obj is already a proper iterator, Iterator.from(obj) returns itself.

    js
    const obj = (function* () {
      yield 1;
      yield 2;
      yield 3;
    })();
    
    const iterator = Iterator.from(obj);
    console.log(iterator === obj); / true
    

    Because obj2 is a non-proper iterator, Iterator.from(obj2) returns a new iterator that wraps the original iterator.

    js
    const obj2 = {
      current: 0,
      next() {
        return { value: this.current++, done: false };
      },
    };
    
    const iterator = Iterator.from(obj2);
    console.log(iterator === obj2); / false
    console.log(iterator.next()); / { value: 0, done: false }
    console.log(obj2.next()); / { value: 1, done: false }
    

    Specifications

    Specification
    Iterator Helpers
    # sec-iterator.from

    Browser compatibility

    See also

    • Polyfill of Iterator.from in core-js
    • es-shims polyfill of Iterator.from
    • Iterator
    • Array.from()

    Help improve MDN

    Iterator object is an abstract base for objects that implement the iterator protocol. It provides methods common to built-in iterators, such as filter(), find(), map(), and reduce(). You can also use the static method Iterator.from() to convert an existing iterable into an Iterator.","name":"Iterator methods"}},"browserCompat":["javascript.builtins.Iterator.from"],"pageType":"javascript-static-method"}}

    Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

    Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant