在Loadash中使用slice命令
我需要编写一个函数,该函数将有一个数组和一个数字,如果数字为正,则该功能将其放在他的位置,这意味着对于数组1,2,3,4,5,数字2将在第二名。 但是,如果数字为负(例如-2),它将从数组的末尾计数(-2将呈现而不是3)
当前我的代码是:
const _ = require("lodash");
const pleasework = (word,value) => {
_.slice(word, [start=0], [end=value.length]) }
console.log(pleasework((5),[1,2,3,4,5,6,7,8,9]))
很喜欢您的帮助,请提前。
I Need to write a function that thats getting an array and a number, if the number is positive then the function will put it inside his place, which means for the array 1,2,3,4,5 the number 2 will be at the 2nd spot.
but if the number is negative (for example -2) it will be counted from the end of the array (-2 will be presented instead of 3)
currently my code is this:
const _ = require("lodash");
const pleasework = (word,value) => {
_.slice(word, [start=0], [end=value.length]) }
console.log(pleasework((5),[1,2,3,4,5,6,7,8,9]))
would love your help, thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我正确理解您的问题,即使不使用
lodash
,您也可以实现它。您可以使用node.js内置方法剪接
。您可以阅读有关
剪接功能采用三个参数,
如果第一个元素是负数,它将从末端插入它。
我希望它有帮助。
剪接
方法更改原始数组的内容,这就是为什么我创建了一个新数组的原因。If I've understood your problem correct, You might be able to achieve it even without using
lodash
. You can use Node.js builtin methodsplice
.you can read about splice method here.
Splice function takes three paramters
If the first element is a negative number it would insert it from the end.
I hope it helps.
The
splice
method changes the content of original array that is why I have created a new array.