reactjs中js中有个列表:
this.state.dialogList
想要实现类似于Python中的range
且是倒叙,5,4,3,2,1的那种
然后去处理。
js python range
【总结】
最后用:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | console.log(` range : start = ${start}, stop = ${stop}, step = ${step}`) let realStop = stop let realStart = start let realStep = step if (typeof realStop = = = 'undefined' ) { / / one param defined realStop = realStart realStart = 0 } if (typeof realStep = = = 'undefined' ) { realStep = 1 } console.log(` range : start = ${start}, stop = ${stop}, step = ${step}`) if ((realStep > 0 && realStart > = realStop) || (realStep < 0 && realStart < = realStop)) { / / return [] return [realStart] } const result = [] for (let i = realStart; realStep > 0 ? i < realStop : i > realStop; i + = realStep) { result.push(i) } return result } |
实现了需要的效果。
其中当:realStart >= realStop
比如:
1 | range(0, 0, -1) |
也可以返回:
1 | [0] |
转载请注明:在路上 » 【已解决】js中实现类似于Python中的Range