Man*_*023 17 javascript arrays angularjs
有没有办法循环使用数组forEach
(不是任何其他类型的循环,我知道如何使用for /标准方法)并且实际上没有反转数组本身?
naa*_*jie 37
var arr = [1, 2, 3];
arr.slice().reverse().forEach(function(x) {
console.log(x);
})
Run Code Online (Sandbox Code Playgroud)
将打印:
3
2
1
Run Code Online (Sandbox Code Playgroud)
arr
仍将是[1, 2, 3]
,.slice()
创造一个浅的副本.
tri*_*cot 20
有一个类似的数组方法,它有一个反向计数器部分,reduce
与reduceRight
:
const array = ['alpha', 'beta', 'gamma'];
array.reduceRight((_, elem) => console.log(elem), null);
Run Code Online (Sandbox Code Playgroud)
将其用于请求的目的时,请确保提供第二个参数。它可以是null
或其他任何东西。另请注意,回调函数将累加器作为第一个参数,您不需要为此目的。
如果包括一个库是一个选项:
洛达什:forEachRight
。
Ale*_*x K 13
只需使用 for 循环。从数组的末尾开始,然后从那里向后。
const array = ['blastoff', 1, 2, 3];
for (let index = array.length - 1; index >= 0; index--) {
const element = array[index];
console.log(element);
}
Run Code Online (Sandbox Code Playgroud)
不,forEach
只有进程通过阵列前进.所以你必须做一些别的事情,你在问题中说过的事情超出了范围.
我可以想到两个只使用前体来使用的选项forEach
(因此,它们不使用for
循环或其他类型的循环).我不知道那些是否超出范围,所以这里是:
复制数组并反转副本,然后使用forEach
它
使用Object.keys
来获取指标,扭转,然后使用forEach
它(将通过该指标,而不是价值循环,但我们可以查找.)
这是#1:
slice
复制数组(浅拷贝,所以不太可能是昂贵的),然后我们反转它,然后forEach
:
var a = ['one', 'two', 'three'];
a.slice().reverse().forEach(function(entry) {
snippet.log(entry);
});
snippet.log("Proof that a is not, itself, reversed: " +
JSON.stringify(a));
Run Code Online (Sandbox Code Playgroud)
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
Run Code Online (Sandbox Code Playgroud)
这是#2:
我们用Object.keys
得到数组索引(使用filter
,如果你保存在你的阵列非元素属性),反说,然后在结果循环:
var a = ['one', 'two', 'three'];
Object.keys(a).reverse().forEach(function(index) {
snippet.log(a[index]);
});
snippet.log("Proof that a is not, itself, reversed: " +
JSON.stringify(a));
Run Code Online (Sandbox Code Playgroud)
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
Run Code Online (Sandbox Code Playgroud)
旁注:filter
如果你的数组中有非元素属性,这就是我的意思:
var a = ['one', 'two', 'three'];
a.nonElementProperty = "foo";
Object.keys(a).filter(function(name) {
return String(+name) === name;
}).reverse().forEach(function(index) {
snippet.log(a[index]);
});
snippet.log("Proof that a is not, itself, reversed: " +
JSON.stringify(a));
Run Code Online (Sandbox Code Playgroud)
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
Run Code Online (Sandbox Code Playgroud)
到目前为止,浏览器似乎还没有优化该Array.forEach
功能。不费吹灰之力,您就可以编写一个简单的 polyfill,Array.forEach
以至少 10 比 1执行该方法。
因此,您可以创建自己的Array.revEach
并使其性能优于本机Array.forEach
,我希望浏览器能Array.forEach
尽快解决性能非常慢的问题,并且不需要对现有的实际方法进行 polyfill。
对于Array.revEach
出进行Array.forEach
中的“铬46.0.2490.22的β-米”运行17倍的速度
if (Array.prototype.revEach === undefined) {
Object.defineProperty(Array.prototype, 'revEach', {
writable : false,
enumerable : false,
configurable : false,
value : function (func) {
var i;
var len = this.length-1;
for (i = len; i >= 0; i--) {
func(this[i], i, this);
}
}
});
}
Run Code Online (Sandbox Code Playgroud)
只是为了添加实际的官方 polyfill 修改为反向。评论显示了我的变化。
// Production steps of ECMA-262, Edition 5, 15.4.4.18
// Reference: http://es5.github.io/#x15.4.4.18
// Modified by Blindman67 to revEach
if (!Array.prototype.revEach) { // name changed
Array.prototype.revEach = function(callback, thisArg) { // name changed
var T; // k defined where len was
if (this == null) {
throw new TypeError(' this is null or not defined');
}
var O = Object(this);
var k = (O.length >>> 0)-1; // set k (counter) ToUint32
// len var removed
if (typeof callback !== "function") {
throw new TypeError(callback + ' is not a function');
}
if (arguments.length > 1) {
T = thisArg;
}
while (k >= 0) { // reverse condition
var kValue;
if (k in O) {
kValue = O[k];
callback.call(T, kValue, k, O);
}
k--; // dec counter
}
};
}
Run Code Online (Sandbox Code Playgroud)
可以使用反向方法,forEach方法和(如果使用ES6)箭头功能相对简洁地完成此操作
var someArray = ["a","b","c","d"];
someArray.reverse().forEach(arrayItem =>
console.log(arrayItem)
)
Run Code Online (Sandbox Code Playgroud)
如果您不使用ES6,则解决方案大致相同,只是没有箭头功能。
var someArray = ["a","b","c","d"];
someArray.reverse().forEach(function(arrayItem) {
console.log(arrayItem)
})
Run Code Online (Sandbox Code Playgroud)
两者都将打印到控制台:
d
c
b
a
Run Code Online (Sandbox Code Playgroud)
array.forEach 有 3 个参数。您可以使用这些来有效地向后移动 forEach。
var arr = [1, 2, 3];
arr.forEach(function(x, index, the_array) {
let x_prime = the_array[the_array.length-1-index]
console.log(x_prime);
})
Run Code Online (Sandbox Code Playgroud)
将打印
3
2
1
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
23629 次 |
最近记录: |