繁体   English   中英

我如何 map 一个从数组末尾到开头的数组?

[英]How can i map an array starting from the end of the array to the beginning?

我想 map 一个数组,但我不想从开始索引开始迭代。 我想从数组的最后一个索引和 go 开始映射数组,一直到开始下降。

如何在 JavaScript 中做到这一点? 有没有 JS 提供的简单方法或者我错过了什么?

您只需在映射之前使用 .reverse() 方法。 这是一个例子:

const array = ["one", "two", "three"];

const reversedMap = [...array].reverse().map(x => {
  return x;
});

这将返回 ["three", "two", "one"]

您当然可以 map 它但是您想要,例如,如果您想要一个无序列表,您可以这样做:

const array = ["one", "two", "three"];

const reversed = [...array].reverse().map(x => {
  return (
    <div>
      <ul>
        <li>{x}</li>
      </ul>
    </div>
  );
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM