Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/403.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何检查字符串中的第一个字符,以及如何在Jquery中将该字符串发送到数组中?_Javascript_Jquery - Fatal编程技术网

Javascript 如何检查字符串中的第一个字符,以及如何在Jquery中将该字符串发送到数组中?

Javascript 如何检查字符串中的第一个字符,以及如何在Jquery中将该字符串发送到数组中?,javascript,jquery,Javascript,Jquery,朋友们 我有一个数组,它包含一些字符串值。 例如:array name=“所有数组” 现在,我想检查该数组中的所有值是否为字符串的第一个字符 如果字符串以字符“a”开头,则将该字符串移动到名为“a_数组”的数组中。 如果字符串以字符“b”开头,则将该字符串移动到名为“b_数组”的数组中 如何完成这项任务 var splitArrays = {}; for(var i = 0; i < All_array.length; ++i){ var firstChar = All_array

朋友们

我有一个数组,它包含一些字符串值。 例如:array name=“所有数组”

现在,我想检查该数组中的所有值是否为字符串的第一个字符

如果字符串以字符“a”开头,则将该字符串移动到名为“a_数组”的数组中。 如果字符串以字符“b”开头,则将该字符串移动到名为“b_数组”的数组中

如何完成这项任务

var splitArrays = {};
for(var i = 0; i < All_array.length; ++i){
    var firstChar = All_array[i].substr(0,1).toUpperCase();
    if(!splitArrays[firstChar + '_array'])
        splitArrays[firstChar + '_array'] = [];
    splitArrays[firstChar + '_array'].push(All_array[i]);
}
等等

这是一把小提琴:

等等


这里有一把小提琴:

代码如下:

for(var i=0; i<All_array.length; i++){
   var firstChar = All_array[i].substr(0, 1).toUpperCase();
   var arrayName = firstChar + "_array";
   if(typeof(window[arrayName]) == 'undefined') window[arrayName] = []; //Create the var if it doesn't exist
   window[arrayName].push(All_array[i]);
}
A_array = []; //empty the array (cause you wanted to 'move')

for(var i=0;i代码如下:

for(var i=0; i<All_array.length; i++){
   var firstChar = All_array[i].substr(0, 1).toUpperCase();
   var arrayName = firstChar + "_array";
   if(typeof(window[arrayName]) == 'undefined') window[arrayName] = []; //Create the var if it doesn't exist
   window[arrayName].push(All_array[i]);
}
A_array = []; //empty the array (cause you wanted to 'move')

for(var i=0;i您可以使用
each()
charAt

$.each(All_array,function(i,s){  
    var c = s.charAt(0).toUpperCase();                
    if(!window[c + '_array']) window[c + '_array'] = [];
    window[c + '_array'].push(s);
});

您可以使用
each()
charAt

$.each(All_array,function(i,s){  
    var c = s.charAt(0).toUpperCase();                
    if(!window[c + '_array']) window[c + '_array'] = [];
    window[c + '_array'].push(s);
});

使用
.substr(0,1)
.charAt(0)更好吗
?有一种方法比另一种更好吗?谢谢!这很好。我需要将'Abcd'、'Anej'、'Aali'推到单独的数组中。我实际上不知道-我从来没有使用过
charAt
。不知道为什么,实际上..我也想知道是否有区别:当第一个是版本3浏览器不支持,但两种浏览器都可以获得完全相同的结果?@Niklas版本3浏览器??哈哈哈,对不起,我忍不住笑了。所以我们不应该使用javascript,因为8086台机器不支持它:S.使用
.substr(0,1)
.charAt(0)更好吗
?有一种方法比另一种更好吗?谢谢!这很好。我需要将'Abcd'、'Anej'、'Aali'推到单独的数组中。我实际上不知道-我从来没有使用过
charAt
。不知道为什么,实际上..我也想知道是否有区别:当第一个是版本3浏览器不支持javascript,但两种浏览器都可以获得完全相同的结果?@Niklas版本3浏览器??哈哈哈,对不起,我忍不住笑了。所以我们不应该使用javascript,因为8086台计算机不支持它:这就是为什么要使用
substr
而不是
substring
,而版本3浏览器不支持第一种浏览器但是,两种版本3的浏览器都可以获得完全相同的结果?哈哈哈。没有注释:DWhy使用
substr
而不是
substring
,当第一种版本3的浏览器不支持时,两种版本3的浏览器都可以获得完全相同的结果?哈哈哈。没有注释:D