Skip to content

Make the underscore wrapper iterable #2447

Open
@jdalton

Description

@jdalton

Thoughts on making the Underscore wrapper an iterator and iterable?

It would require detecting Symbol.iterator and doing something like

_.prototype[Symbol.iterator] = function() {
  return this;
};
_.prototype.next = function() {
  if (this.__values__ === undefined) {
    this.__values__ = _.toArray(this.value());
  }
  var done = this.__index__ >= this.__values__.length,
      value = done ? undefined : this.__values__[this.__index__++];
  return { 'done': done, 'value': value };
};

Related to jashkenas/backbone#3954 (comment).

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions