Javascript-使用forEach向后遍历数组 [英] Javascript - Loop through array backwards with forEach

查看:121
本文介绍了Javascript-使用forEach向后遍历数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以使用forEach向后循环遍历数组(没有其他任何类型的循环,我知道如何使用for/标准方法)并且实际上没有反转数组本身?

Is there a way to loop backwards through an array using forEach (not any other kind of loop, I know how to do with with a for / standard ways) and without actually reversing the array itself?

推荐答案

var arr = [1, 2, 3];

arr.slice().reverse().forEach(function(x) {
    console.log(x);
})

将打印:

3
2
1

arr仍为[1, 2, 3].slice()创建浅表副本.

arr will still be [1, 2, 3], the .slice() creates a shallow copy.

这篇关于Javascript-使用forEach向后遍历数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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