Javascript反转数字数组,然后减去2,然后是1,然后是0,再加1,再加2等等

Javascript reverse array of numbers and subtract 2, then 1, then 0 then add 1, add 2 etc

本文关键字:然后 再加 等等 数字 数组 Javascript      更新时间:2023-09-26

很抱歉标题太糟糕了。我已经崩溃了足够多的浏览器并请求帮助。

这是我想解决的具体问题。

创建一个名为"reversedLooper"的函数,当传递该函数时,数组将反向循环通过该函数,并从最后一个元素减去2,从倒数第二个元素减去1,从倒数倒数第三个元素减去0,在倒数第四个元素加1,从最后第五个元素加2,等等,直到它到达数组的前面。完成后返回列表

使用[].map:很容易

myArray.map(function(item, index, arr) {
  return item + arr.length - index - 3;
});

这里有一个更短、更简洁的版本。

function reversedLooper(arr){
  var increaseBy = -2;
  for(var i = arr.length - 1; i >= 0; i--){
    arr[i] += increaseBy;
    increaseBy++
  }
  return arr;
}

它确实很难看,但似乎满足了测试用例。

var array = [1,2,3,4,5,6,7,8,9,10,11,12,13];
function reversedLooper(array) {
    var newArray = [];
    var n = -3;
    for (var i = array.length - 1; i >= 0; i--) {
      n++;
        if (array[i] === array.length) {
            newArray.push(array[i] - 2);
        }
        else if (array[i] === array.length - 1) {
            newArray.push(array[i] - 1);
        }
        else if (array[i] === array.length - 2) {
            newArray.push(array[i]);
        }
        else {
            newArray.push(array[i] + n)
        }
    }
    return newArray;
}
console.log(reversedLooper(array));

在我的评论的基础上,我将使用新的fiddle插件来制作一些东西,在这里向您展示。

let list = Immutable.List.of(1,2,3,4,5)
function createListOperationsMapping(list) {
  // Maybe consider dynamically creating a mapping of methods to indexes
  // If you return a Map from this that is something like:
  /*
   Map({
     0: (item) => item - 2,
     1: (item) => item - 1,
     // since there's a case where you subtract 0, so no change
     2: (item) => item,
     3: (item) => item + 1,
     4: (item) => item + 2,
     5: (item) => item + 3,
     // etc...
   })
   
   */
  // This would allow you to map the reversedList and based on the index you could just  
  // call the method from the map...
  // Like this (where the map from above ^^ is called listOperationMap)
  /*
  
    var mutatedReversedList = reversedList.map((item, index) => {
      return listOperationMap[index](item);
    })
   Notice I used the word "mutated" here. If you use Immutable, 
   you're actually returning a copy with updates rather than mutating, 
   but I wanted to make it clear that the result of mapping the 
   Immutable List will be a new Immutable List - not the mutated 
   previous list. Also, if you choose to work with Immutable, remember 
   you will need to set new variables for return values of updates to 
   the Immutable data structures.
  
   */
}
let reversedList = list.reverse() 
let reversedListSize = reversedList.size
let newList = reversedList.map((listItem, index) => {
  // Do stuff in here
})
console.log(list.toJS())
console.log(reversedList.toJS())
console.log(reversedListSize)
<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.7.6/immutable.min.js"></script>

由于你的问题有点模糊,我基本上只是根据需要解决的问题提出一些想法。希望这至少在开始探索如何处理您试图解决的问题时有用。

无论哪种方式,我都建议查看Immutable.js.

附言:如果你点击"运行代码片段",然后打开你的开发工具,控制台日志就会显示在你的控制台中。