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

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

问题描述

在ES6中,我们现在有迭代器和<一个href =https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/for...of =noreferrer> for..of 来迭代它们。我们有一些内置的数组的内置功能;特别是密钥条目



这些方法允许执行一般通常执行的大部分迭代。但是,反过来呢呢?这也是一个非常常见的任务,在规范专门为它?或者也许我错过了吗?



好的,我们有 Array.prototype.reverse ,但是我不一定要反转一个大数组,然后在完成后再次反转。我也不想使用 Array.prototype .slice 做一个临时的浅层复制,反转只是为了迭代。



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



< pre class =snippet-code-js lang-js prettyprint-override> (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,ar [R [键]]; }} var pre = document.getElementById('out'); function log(result){pre.appendChild(document.createTextNode(result +'\\\
')); } var a = ['a','b','c']; for(var x of reverseKeys(a)){log(x); } log(''); for(var x of reverseValues(a)){log(x); } log(''); for(varX of reverseEntries(a)){log(x); }}());

 < pre id = out>< / pre>  



反向迭代的方法是在ES6中使用,或者我错过了规范中的一些基础?

解决方案


这是真的反向迭代的意图在ES6?


有一个反向迭代的建议,讨论在 esdicuss 和一个git项目,概述了一个 spec ,但是对于它而言似乎没有什么好处。 ES6现在已经定稿了,所以这次不会再添加。无论如何,对于数组和字符串,我写了一些代码来填补空白(在我看来),我会把它发布在这里,因为它可以帮助别人。这个代码是基于我今天的浏览器,如果在其上实现了更多的ES6,那么可能会有一些改进。稍后我可能会想到一个gist或一个小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天全站免登陆