Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JS Array.splice return original Array and chain to it

I have a string with values like this a,b,c,d and want to remove a specific letter by index

So here is what I did str.split(',').splice(1,1).toString() and this is (obviously) not working since splice is returning the values removed not the original array

Is there any way to do the above in a one liner?

var str = "a,b,c,d";
console.log(str.split(',').splice(1,1).toString());

Thanks in advance.

like image 786
Moses Schwartz Avatar asked Dec 11 '22 00:12

Moses Schwartz


1 Answers

You can use filter and add condition as index != 1.

var str = "a,b,c,d";
console.log(str.split(',').filter((x, i) => i != 1).toString());
like image 169
Karan Avatar answered Jan 13 '23 23:01

Karan