我有很长的csv数据......比如1,2,3,........,120 我想检查它是否是最后一个。但是如何用javascript做到这一点?
这里iam拆分所有csv并将每个csv分开。
var movieSRC = CSV;
if (movieSRC.indexOf(',') > -1) {
movieSRC = movieSRC.split(',');
for (var i = 0; i < movieSRC.length; i++) {
***//need to check if it is a last one!!!***
movies.push(movieSRC[i]);
}
答案 0 :(得分:2)
//Using split() method
var arr = movieSRC.split(','); //give you an array
var element = arr[arr.length-1]; //get the last element of array
//Ussing substring() and lastIndexOf()
var element = movieSRC.substring(movieSRC.lastIndexOf(',')+1);
正如评论中所建议的那样,你也可以使用Array.pop()方法,但它会从数组中删除元素:
var element = movieSRC.split(',').pop();