cantaffordavan
cantaffordavan

Reputation: 1447

First split then slice value

I know how to split a value using deliminators. I also know how to slice a value to remove the last couple chars. I need to combine these now, which is the part I can't figure out.

    $("#product").val(value.split('|')[0]);

How would I add slice(0,-1) to this function above? I want it to first split, then slice.

So the result would be everything before the '|' and minus one char

Solved:

$("#product").val(value.split('|')[0].slice(0, -1))

Upvotes: 2

Views: 1832

Answers (2)

Ram
Ram

Reputation: 144679

try this:

var val = value.split('|')[0].slice(0, -1)     
$("#product").val(val);

Upvotes: 1

Selvakumar Arumugam
Selvakumar Arumugam

Reputation: 79830

You mean slice the 1st element from array?

Try below,

 $("#product").val(value.split('|')[0].slice(0, -1))

Upvotes: 3

Related Questions