Javascript过滤器是否保留订单? [英] Does Javascript filter preserve order?

查看:87
本文介绍了Javascript过滤器是否保留订单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我查看了文档,在查看示例时,看起来过滤器保留了原始列表的顺序(尽管它返回一个新的)。我能依靠吗?

I looked at the documentation and while looking at examples it looks like filter preserves order of the original list (though it returns a new one). Can I rely on that?

Ecmascript规范

MDN

这些参考保留顺序都不是。我应该假设我不能依赖保留的订单吗?

Neither of these reference preserving order. Should I just assume I can't rely on preserved order?

推荐答案

是的。来自规范



  • 选择是使用调用 callbackfn 的[[Call]]内部方法的结果T 作为此值和参数列表,包含 kValue k O

  • 如果 ToBoolean 已选中 true ,然后

  • Let selected be the result of calling the [[Call]] internal method of callbackfn with T as the this value and argument list containing kValue, k, and O.
  • If ToBoolean(selected) is true, then


  • 调用带有参数的 ToString ),属性描述符 {[ [值]]: kValue ,[[Writable]]: true ,[[Enumerable]]: true ,[[Configurable]]: true }, false

  • 增加到 1。

  • Call the [[DefineOwnProperty]] internal method of A with arguments ToString(to), Property Descriptor {[[Value]]: kValue, [[Writable]]: true, [[Enumerable]]: true, [[Configurable]]: true}, and false.
  • Increase to by 1.

因此返回数组中的项目与原始项目中的项目顺序相同。

So the items in the returned array have the same order than in the original one.

这篇关于Javascript过滤器是否保留订单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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