Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/445.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)_Javascript_Arrays - Fatal编程技术网

取出数组中每个元素的第一个和最后一个字母(javascript)

取出数组中每个元素的第一个和最后一个字母(javascript),javascript,arrays,Javascript,Arrays,我需要取出数组1中每个元素的第一个字母和最后一个字母,并将其推到空数组2。我不知道转换成字符串后该怎么办。我想我需要使用for循环来遍历它,那么我如何拉出每个元素中的第一个和最后一个字母并推到EmptyArray 2 循环遍历数组并返回字符串减去第一个字符和最后一个字符。 map函数将创建一个新数组,子字符串将索引1中的所有字符添加到字符串-1的总长度中 var array1 = ["hello", "sam", "how"]; var emptyArray2 = []; array1.jo

我需要取出数组1中每个元素的第一个字母和最后一个字母,并将其推到空数组2。我不知道转换成字符串后该怎么办。我想我需要使用for循环来遍历它,那么我如何拉出每个元素中的第一个和最后一个字母并推到EmptyArray 2

循环遍历数组并返回字符串减去第一个字符和最后一个字符。 map函数将创建一个新数组,子字符串将索引1中的所有字符添加到字符串-1的总长度中

var array1 = ["hello", "sam", "how"];

var emptyArray2 = [];

array1.join(" ");
更多方法可以做到这一点:

//创建一个新数组 让数组=[你好,山姆,你好]; //从数组项中查找第一个字母。 让firstLetters1=array.mapeachItemInArray=>{ 返回每个数组[0]; } //显示结果 console.log数组中项目的首字母为:; console.logfirstLetters1; //用一行代码编写上述代码的另一种捷径。 让firstLetters2=array.mapeachItemInArray=>eachItemInArray[0]; //显示结果 console.log数组中项目的第一个字母是快捷方式:; console.logfirstLetters2; //从数组项中查找最后一个字母。 让lastLetters1=array.mapeachItemInArray=>{ 让index=eachItemInArray.length-1; 返回每个数组[索引]; } //显示结果 console.logLast数组中项目的字母为:; console.loglastLetters1; //在一行中编写上述代码的另一种捷径 让lastLetters2=array.mapeachItemInArray=>eachItemInArray[eachItemInArray.length-1]; //显示结果 console.logLast数组中项目的字母为快捷方式:;
console.loglastLetters2;可以使用str[i]将字母转换成字符串。至于推入数组,搜索推入数组javascript,我只需要第一个和最后一个。那么剩下的部分该怎么处理呢?@user3525853预期的输出是什么?预期的输出是:h,s,h@user3525853最后的字符呢?这不会删除每个元素中的第一个和最后一个字母。我的错,我认为应该只删除第一个字符,我更新了子字符串。您应该使用.charAt而不是字符串索引。@jfriend00当然,但是你能告诉我为什么吗?@thefortheye,基本上看,IE7不支持字符串索引。@jfriend00如果你考虑的是非BMP字符,我认为字符和字符串索引一样有限,主要是因为字符串的[]不是早期标准的一部分,因此没有得到普遍支持。但是,也因为通过var chr=str[x]访问意味着你可以通过str[x]=f分配,你不能只是默默地失败,所以我认为这会让人们更容易犯错误,认为某些东西像数组一样工作,而实际上它不是。我想我会说它现在更多的是一种风格选择,除非你想支持IE7,在这种情况下你必须使用.charAt。
var array2 =  ["hello", "sam", "how"].map(function(p){return p.substring(1, p.length - 1)})
var array1 = ["hello", "sam", "how"], result = [];
array1.forEach(function(currentString) {
    result.push(currentString.charAt(0));  // First character
    result.push(currentString.charAt(currentString.length - 1)); //Last character
});
console.log(result);
# [ 'h', 'o', 's', 'm', 'h', 'w' ]
var array1 = ["hello", "sam", "how"];

var emptyArray2 = [];

array1.forEach(function(item){
 //item.charAt(0) will give character at start of string
 //item.charAt(item.length-1) will give character at end of string
 emptyArray2.push(item.charAt(0),item.charAt(item.length-1));
 //Array.push(..) is to append elements into the array
});
console.log(emptyArray2); // will print ["h","o","s","m","h","w"] in console