ES6使用for..of反向迭代一个数组,我错过了在规范的东西吗? [英] ES6 reverse iterate an array using for..of, have I missed something in the spec?

查看:181
本文介绍了ES6使用for..of反向迭代一个数组,我错过了在规范的东西吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ES6我们现在有迭代器和< A HREF =htt​​ps://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/for...of相对=nofollow> for..of 迭代他们。我们有一些内建的阵列;值得注意的是,的和<一个href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/entries\"相对=nofollow>项的。

这些方法允许之一来执行多少人会经常进行迭代。但是,这个反向迭代?这也是一个很常见的任务,我没有看到任何东西在规范专门为它?或者,也许我错过了?

好吧,我们有 Array.prototype.reverse ,但我不一定要扭转地方大阵,然后在完成时再次扭转这种局面。我也不想使用 Array.prototype .slice 做一个临时的浅复制和反向只是迭代。

所以我看了看一个发电机并想出了这些工作的解决方案。

\r
\r

(函数(){\r
  使用严格的;\r
\r
  功能* reverseKeys(ARR){\r
    让键= arr.length - 1;\r
\r
    而(按键&GT; = 0){\r
      产量的关键;\r
      关键 - = 1;\r
    }\r
  }\r
\r
  功能* reverseValues​​(ARR){\r
    对于(让reverseKeys的关键(ARR)){\r
      产量ARR [关键]\r
    }\r
  }\r
\r
  功能* reverseEntries(ARR){\r
    对于(让reverseKeys的关键(ARR)){\r
      产量[键,编曲[关键]];\r
    }\r
  }\r
\r
  VAR pre =的document.getElementById('出');\r
\r
  功能日志(结果){\r
    pre.appendChild(document.createTextNode(结果+的'\\ n'));\r
  }\r
\r
  变种一个= ['一','B','C'];\r
\r
  为(reverseKeys的变种X(一)){\r
    日志(X);\r
  }\r
\r
  日志('');\r
  为(reverseValues​​的变种X(一)){\r
    日志(X);\r
  }\r
\r
  日志('');\r
  为(reverseEntries的变种X(一)){\r
    日志(X);\r
  }\r
}());

\r

&LT; pre ID =走出去&GT;&LT; / pre&GT;

\r

\r
\r

这是真的反向迭代拟在ES6或有我​​错过了在规范的东西根本的方式?


解决方案

  

这是真的要彻底扭转迭代的方式拟在ES6?


有是反向迭代的建议,在 esdicuss 和Git项目概述了讨论< A HREF =htt​​ps://github.com/leebyron/ecmascript-reverse-iterable相对=nofollow>规范,但没有什么似乎对于这样的情况发生。 ES6现在最终确定,所以它不是将要围绕增加这个时候的东西。总之,对于数组和字符串我写了一个小code,以填补空白(在我看来),我将它张贴在这里,因为它可能帮助别人。这code今天是根据我的浏览器,如果有更多的ES6对他们实施了一些改进,也可能会被提出。我可以避开一个要点或更高版本的小GitHub的项目。

更新:我创建了一个的GitHub 项目这方面的工作。

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

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?

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.

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>

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

解决方案

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

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.

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

这篇关于ES6使用for..of反向迭代一个数组,我错过了在规范的东西吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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