简体   繁体   English

从JavaScript中的单词数组中查找第n个字符

[英]Find the nth character from an array of words in JavaScript

I'd like to create a function (nthChar) that takes 1 parameter - an array of n words. 我想创建一个带有1个参数的函数(nthChar)-n个单词的数组。

The function should concatenate the nth letter from each word to construct a new word which should be returned as a string. 该函数应连接每个单词的第n个字母以构造一个新单词,该单词应作为字符串返回。

The nth letter should be the 1st from the first word in the array, the second from the second word in the array, the third from the third and so on. 第n个字母应该是数组中第一个单词的第一个字母,第二个单词是数组中第二个单词的字母,第三个字母是第三个字母,依此类推。 So: nthChar(['I','am','Tom']) should return 'Imm' 因此:nthChar(['I','am','Tom'])应该返回'Imm'

Here's my attempt: function nthChar(words){ 这是我的尝试:函数nthChar(words){

for (var i=0; i<words.length; i++) {
return words[i].charAt(words.indexOf(words[i]))
}
}

Which only seems to grab the first letter of the first word. 似乎只能抓住第一个单词的第一个字母。 How would I proceed to the other words of the array before concatenation? 在串联之前,如何处理数组的其他字词?

With minimal changes to your code, you can do this 只需对代码进行最少的更改,即可完成此操作

function nthChar(arr) {
    var str = '';
    for (var i=0; i<words.length; i++) {
        str = str + words[i][i];
    }
    return str;
}

str - used to build up the result string str用于构建结果字符串

words[i] selects the i'th word ... the second [i] in that statement selects the i'th letter in that word words[i]选择第i个单词...该语句中的第二个[i]选择该单词中的第i个字母

for example: "Hello World"[6] is W 例如: "Hello World"[6]W

Bonus: works in IE8 and earlier ... 奖励:在IE8和更早版本中可以使用...


and, just for the hell of it, void 's answer in ES6 并且,就其地狱而言,ES6中的void答案是void

var nthChar = arr => arr.map((i, v) => i[v]).join('');
function nthChar(arr){

  return arr.map(function(i, v){
      return i[v];
  }).join("");

}

console.log(nthChar(['I','am','Tom']));

So it is returning an array of the characters you want and then it is joining it. 因此,它将返回所需字符的数组,然后将其加入。 Makes sense? 说得通?

The issue with your code was that you were not concatenating anything to the output. 代码的问题是您没有将任何内容串联到输出中。 You can access the characters of a string as if it is an array . 您可以像对待array一样访问string的字符。

Live Fiddle 现场小提琴

Your code can be: 您的代码可以是:

function nthChar(words){
    var str = "";
    for (var i=0; i<words.length; i++) {
       str += words[i].charAt(i);
    }
    return str;
}

Fiddle 小提琴

Try something like this 试试这个

function nthChar(words){
  var result = "";
  for (var i = 0, ln = words.length; i<ln; i++) 
    result += words[i].charAt(i);
  return result;
}

This can be as simple as 这可以很简单

function nthChar(words){
    var s = "";
    for(var i=0;i<words.length;i++){
      s+= words[i].charAt(i);
    }
    return s;
}

Live example: http://jsfiddle.net/11yc79jn/ 实时示例: http//jsfiddle.net/11yc79jn/

This is closest to your original solution, and just uses the loop control variable i which is incrementing already as you loop through the words array. 这是最接近原始解决方案的方法,只使用循环控制变量i ,当循环遍历words数组时,该变量已经在增加。 Of course, also return after the entire loop has run as well. 当然,在整个循环运行之后也要返回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM