如何在不使用库的情况下反转 JavaScript 中的数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10168034/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 00:08:40  来源:igfitidea点击:

How can I reverse an array in JavaScript without using libraries?

javascriptarrays

提问by u283863

I am saving some data in order using arrays, and I want to add a function that the user can reverse the list. I can't think of any possible method, so if anybody knows how, please help.

我正在使用arrays按顺序保存一些数据,并且我想添加一个用户可以反转列表的功能。我想不出任何可能的方法,所以如果有人知道如何,请帮忙。

回答by Andreas Wong

Javascript has a reverse()method that you can call in an array

Javascript 有一个reverse()可以在数组中调用的方法

var a = [3,5,7,8];
a.reverse(); // 8 7 5 3

Not sure if that's what you mean by 'libraries you can't use', I'm guessing something to do with practice. If that's the case, you can implement your own version of .reverse()

不确定这是否就是您所说的“您不能使用的库”的意思,我猜这与实践有关。如果是这种情况,您可以实现自己的版本.reverse()

function reverseArr(input) {
    var ret = new Array;
    for(var i = input.length-1; i >= 0; i--) {
        ret.push(input[i]);
    }
    return ret;
}

var a = [3,5,7,8]
var b = reverseArr(a);

Do note that the built-in .reverse()method operates on the original array, thus you don't need to reassign a.

请注意,内置.reverse()方法对原始数组进行操作,因此您无需重新分配a

回答by Rohit Shelhalkar

Array.prototype.reverse()is all you need to do this work. See compatibility table.

Array.prototype.reverse()这就是您完成这项工作所需的全部内容。请参阅兼容性表

var myArray = [20, 40, 80, 100];
var revMyArr = [].concat(myArray).reverse();
console.log(revMyArr);
// [100, 80, 40, 20]

回答by Jake Boyles

Heres a functional way to do it.

这是一个功能性的方法来做到这一点。

const array = [1,2,3,4,5,6,"taco"];

function reverse(array){
  return array.map((item,idx) => array[array.length-1-idx])
}

回答by Torkel Velure

20 bytes

20 字节

let reverse=a=>[...a].map(a.pop,a)

回答by qwertymk

This is what you want:

这就是你想要的:

array.reverse();

DEMO

演示

回答by Frambot

> var arr = [1,2,3,4,5,6];
> arr.reverse();
  [6, 5, 4, 3, 2, 1]

回答by enjareyes

array.reverse() 

Above will reverse your array but modifying the original. If you don't want to modify the original array then you can do this:

以上将反转您的数组,但修改原始数组。如果您不想修改原始数组,则可以执行以下操作:

var arrayOne = [1,2,3,4,5];

var reverse = function(array){
    var arrayOne = array
    var array2 = [];

    for (var i = arrayOne.length-1; i >= 0; i--){
      array2.push(arrayOne[i])
    } 
    return array2
}

reverse(arrayOne)

回答by user2914136

Here is a version which does not require temp array.

这是一个不需要临时数组的版本。

function inplaceReverse(arr) {
  var i = 0;
  while (i < arr.length - 1) {
    arr.splice(i, 0, arr.pop());
    i++;
  }
  return arr;
}

// Useage:
var arr = [1, 2, 3];
console.log(inplaceReverse(arr)); // [3, 2, 1]

回答by Elia Grady

The shortest reverse method I've seen is this one:

我见过的最短的反向方法是这个:

let reverse = a=>a.sort(a=>1)

回答by parag patel

**

**

Shortest reverse array method without using reverse method:

不使用反向方法的最短反向数组方法:

**

**

 var a = [0, 1, 4, 1, 3, 9, 3, 7, 8544, 4, 2, 1, 2, 3];

 a.map(a.pop,[...a]); 
// returns [3, 2, 1, 2, 4, 8544, 7, 3, 9, 3, 1, 4, 1, 0]

a.pop method takes an last element off and puts upfront with spread operator ()

a.pop 方法取下最后一个元素并使用扩展运算符 () 放在前面

MDN links for reference:

MDN 链接供参考:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/pop

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/pop