ES6 使用 for..of 反向迭代数组,我是否遗漏了规范中的某些内容? [英] ES6 reverse iterate an array using for..of, have I missed something in the spec?

查看:22
本文介绍了ES6 使用 for..of 反向迭代数组,我是否遗漏了规范中的某些内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 ES6 中,我们现在有 迭代器for..of 来迭代它们.我们有一些数组的内置函数;尤其是 keysvaluesentries.

In ES6 we now have iterators and for..of to iterate them. we have some built-ins for arrays; notably keys, values and entries.

这些方法允许人们执行通常会执行的大部分迭代.但是,反向迭代呢?这也是一项非常常见的任务,我在 spec 专门针对它?或者我错过了?

These methods allow one to perform much of the iteration one would commonly perform. But, what about iteration in reverse? This is also a very common task and I don't see anything in the spec specifically for it? Or maybe I missed it?

好的,我们有 Array.prototype.reverse 但我不一定要在原地反转一个大数组,然后在完成后再次反转它.我也不想使用 Array.prototype.slice 制作一个临时的浅拷贝并反转它只是为了迭代.

Ok, we have Array.prototype.reverse but I don't necessarily want to reverse a large array in place and then reverse it again when finished. I also don't want to use Array.prototype.slice to make a temporary shallow copy and reverse that just for iteration.

所以我看了一下generators 和提出了这些可行的解决方案.

So I took a look a generators and came up with these working solutions.

(function() {
  'use strict';

  function* reverseKeys(arr) {
    let key = arr.length - 1;

    while (key >= 0) {
      yield key;
      key -= 1;
    }
  }

  function* reverseValues(arr) {
    for (let key of reverseKeys(arr)) {
      yield arr[key];
    }
  }

  function* reverseEntries(arr) {
    for (let key of reverseKeys(arr)) {
      yield [key, arr[key]];
    }
  }

  var pre = document.getElementById('out');

  function log(result) {
    pre.appendChild(document.createTextNode(result + '\n'));
  }

  var a = ['a', 'b', 'c'];

  for (var x of reverseKeys(a)) {
    log(x);
  }

  log('');
  for (var x of reverseValues(a)) {
    log(x);
  }

  log('');
  for (var x of reverseEntries(a)) {
    log(x);
  }
}());

<pre id="out"></pre>

这真的是 ES6 中的反向迭代方式,还是我遗漏了规范中的一些基本内容?

Is this really the way that reverse iteration is intended in ES6 or have I missed something fundamental in the spec?

推荐答案

这真的是 ES6 中的反向迭代方式吗?

Is this really the way that reverse iteration is intended in ES6?

有一个关于反向迭代的提议,在 esdicuss 和一个概述了<一个 href="https://github.com/leebyron/ecmascript-reverse-iterable" rel="noreferrer">spec,但似乎没有发生什么.ES6 现在已经完成,所以这次不会添加它.无论如何,对于数组和字符串,我已经编写了一些代码来填补空白(在我看来),我会在这里发布它,因为它可能会帮助其他人.这段代码基于我今天的浏览器,如果在它们上面实现了更多的 ES6,可能会做出一些改进.稍后我可能会考虑一个要点或一个小的 github 项目.

There was a proposal for reverse iteration, discussed on esdicuss and a git project outlining a spec, but nothing much seemed to happen with respect to it. ES6 is finalised now, so it's not something that is going to be added this time around. Anyway, for arrays and strings I've written a little code to fill in the gaps (in my opinion) and I will post it here as it may help others. This code is based on my browsers today and some improvements could possibly be made if there was more of ES6 implemented on them. I may get around to a gist or a small github project later.

更新:我已经创建了一个 GitHub 项目来解决这个问题.

Update: I have created a GitHub project for work on this.

这篇关于ES6 使用 for..of 反向迭代数组,我是否遗漏了规范中的某些内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆