Getting the last element of a split string array

object,tipjs,es6

1 - Ask javascript

I need to get the last element of a split array with multiple separators. The separators are commas and space. If there are no separators it should return the original string. If the string is "how,are you doing, today?" it should return "today?" If the input were "hello" the output should be "hello". How can I do this in JavaScript? 

2 - Answer

There's a one-liner for everything. :)

var output = input.split(/[, ]+/).pop();

or

var item = "one,two,three";
var lastItem = item.split(",").pop();
console.log(lastItem); // three

Có thể bạn đã miss một số snippets code

Rest Operator for Objects

Now we can use rest on the properties of an Object. It allows us to explicitly extract certain named variables, and assign any uncalled variables into a catchall Object.

Tránh viết biến có phạm vi global

Đôi khi bạn cần phải viết một tập lệnh trực tiếp trên trang. Cần lưu ý rằng trong code của bạn vô tình khai báo một biến toàn cầu. Vì phạm vi toàn cầu phức tạp hơn nên việc tìm kiếm chậm hơn rất nhiều

Using promises to catch errors

Sử dụng reduce và concat làm phẳng một array

Array.reduce() và Array.concat() có thể giúp chúng ta làm phẳng một Array như:

Random Number javascript

Random Number javascript