Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make splice or unshift return the modified array

I am recently getting accustomed to the idea of chaining the function calls and it is making my life a lot easier. But the inbuilt Array functions of JavaScript don't seem too cooperative.

How to make unshift or splice return the modified rather than the length of array and deleted items respectively? I am assuming there is a way to this with some tricky calls to call or apply.

I had to go out of my way and use concat for my use case, is there a better way of doing it?

Eg: 
 a = [2,3,4,5]
 one solution : somethingElse([1].concat(a))
 I wanted to do something like:  somethingElse(a.unshift(1));
 or may be like this: somethingElse(a.splice(0,0,1));
like image 908
sasidhar Avatar asked Jul 15 '26 08:07

sasidhar


1 Answers

Use grouping by parentheses, f.e. to return modified array after slice:

var a = [2,3,4,5];
somethingElse((a.splice(0,0,1), a));
like image 141
Kirill Pisarev Avatar answered Jul 17 '26 22:07

Kirill Pisarev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!