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

Làm phẳng mảng dùng phương pháp đệ quy

Làm phẳng mảng dùng phương pháp đệ quy

Chuyển đổi thời gian trong javascript

Sử dụng Object.entries(), Array.prototype.filter() và Array.prototype.map(). Trả về định dạng ngày, giờ, phút với một miliseconds đã cho.

Promise.all là gì?

Promise.all là gì? Hiểu và sử dụng promise all trong những trường hợp nào trong vòng 1 phút.

Sự khác biệt giữa substr và substring

Sự khác biệt giữa substr() và substring() chức năng trong JS là gì?

serialize Form

Sử dụng formdata constructor để convert form, FormData, array.from(). Kết hợp window.encodeURIComponent() để encode từng value trong form.