Javascript反转数字数组,然后减去2,然后是1,然后是0,再加1,再加2等等
Javascript reverse array of numbers and subtract 2, then 1, then 0 then add 1, add 2 etc
很抱歉标题太糟糕了。我已经崩溃了足够多的浏览器并请求帮助。
这是我想解决的具体问题。
创建一个名为"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.
附言:如果你点击"运行代码片段",然后打开你的开发工具,控制台日志就会显示在你的控制台中。
相关文章:
- 多个下载链接到一个zip文件,然后再下载javascript
- 先在Angular中加载配置文件,然后再加载其他文件
- 先加载特定的图像,然后再加载页面的其余部分
- Javascript反转数字数组,然后减去2,然后是1,然后是0,再加1,再加2等等
- 先卸载页面,然后再重新加载
- 预加载等待 3 秒钟,然后再打开页面
- 请等待异步操作 (firebase) 调用完成,然后再加载网页
- Jasmine - 等待异步库完全加载,然后再执行测试
- RequireJS - 先初始化一个依赖模块,然后再加载其他模块
- “预加载”苹果触摸预组合图标,然后再添加到主屏幕
- 如何在应用程序模板中先加载 Ember 组件,然后再加载其他所有内容
- jQuery.load()等待,然后再加载内容
- 在javascript事件上预加载一个文件,然后再显示它
- 动态加载jQuery,然后再加载Typeahead.js
- JavaScript等待,直到一个图像完全加载,然后再继续脚本
- 如果切换img src然后再返回,图像将被加载两次
- 将24位十六进制数转换为十进制,加1,然后再转换回来
- 重新加载页面一次,然后再重新加载,不改变内容
- 正在等待jquery异步加载,然后再加载js
- 先加载一个外部javascript文件,然后再加载另一个