javascript 在Javascript中获取数组中每个字符串的第一个字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47035752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting the first characters of each string in an array in Javascript
提问by Hyman coltrane
let animals = ['Hen', 'elephant', 'llama', 'leopard', 'ostrich', 'Whale', 'octopus', 'rabbit', 'lion', 'dog'];
let secretMessage = animals.map(function(animal) {
for(animal = 0; animal <= animals.length-1; animal++) {
return animals[animal].charAt(animal);
}
});
console.log(secretMessage.join(''));
Hi, thru this piece of code i want to output the string HelloWorld, which is formed by the first characters of each string/element in the animals array. However, the output is instead HHHHHHHHHH. I don't know if the for loop is the problem here?
嗨,通过这段代码,我想输出字符串HelloWorld,它由动物数组中每个字符串/元素的第一个字符组成。但是,输出是HHHHHHHHHH。不知道是不是for循环的问题?
Could someone please tell me why the code produces such an output and how i can modify it in order for the desired result to be returned successfully?
有人可以告诉我为什么代码会产生这样的输出以及我如何修改它以便成功返回所需的结果?
I'm just a novice for now & that's why your help will play a tremendous part in my growth as a programmer. Thanks in advance!
我现在只是一个新手,这就是为什么您的帮助将在我作为程序员的成长中发挥巨大作用的原因。提前致谢!
回答by Arman Charan
Mapis a for loopin and of itself.
Try:
尝试:
// Animals.
const animals = ['Hen', 'elephant', 'llama', 'leopard', 'ostrich', 'Whale', 'octopus', 'rabbit', 'lion', 'dog']
// Secret Message.
const secretMessage = animals.map((animal) => animal[0]).join('')
// Log.
console.log(secretMessage) // "HelloWorld"
Or alternatively:
或者:
// Animals.
const animals = ['Hen', 'elephant', 'llama', 'leopard', 'ostrich', 'Whale', 'octopus', 'rabbit', 'lion', 'dog']
// Secret Message.
const secretMessage = animals.reduce((accumulator, animal) => accumulator + animal[0], '')
// Log.
console.log(secretMessage) // "HelloWorld"
回答by Joseph Carrick
The map method does the for loop for you. So all you need in the function is animal[0]
map 方法为您执行 for 循环。所以你在函数中需要的只是animal[0]
let secretMessage = animals.map(function(animal) {
return animal[0]
});
This will return every first character in every string in the array, animals. Everything else you have is right.
这将返回数组中每个字符串中的第一个字符,animals。你拥有的其他一切都是对的。
回答by dfsq
You don't need inner loop:
你不需要内循环:
let animals = ['Hen', 'elephant', 'llama', 'leopard', 'ostrich', 'Whale', 'octopus', 'rabbit', 'lion', 'dog'];
let secretMessage = animals.map(function(animal) {
return animal.charAt(0); // or simply animal[0]
});
console.log(secretMessage.join(''));
回答by Luca De Nardi
The error here is that you have a loop inside the map function, which already loops the array.
这里的错误是你在 map 函数中有一个循环,它已经循环了数组。
The correct code is
正确的代码是
animals.map(x=>x[0]).join('');
//or
animals.map(function(animal){
return animal[0]; //First letter of word
}).join(''); //Array -> String
回答by Kenji Mukai
This should be the code:
这应该是代码:
let animals = ['Hen', 'elephant', 'llama', 'leopard', 'ostrich', 'Whale', 'octopus', 'rabbit', 'lion', 'dog'];
let secretMessage = animals.map(function(animal) {
return animal.charAt(0);
});
console.log(secretMessage.join(''));
回答by marmeladze
Just do this.
就这样做。
let animals = [
'Hen', 'elephant', 'llama',
'leopard', 'ostrich', 'Whale',
'octopus', 'rabbit', 'lion', 'dog'
];
console.log(animals.map(e => e[0]).join(""));
回答by Kenny Heenatigala
let animals = ['Hen', 'elephant', 'llama', 'leopard', 'ostrich', 'Whale', 'octopus', 'rabbit', 'lion', 'dog'];
let secretMessage = animals.map(function(animals) {
return animals[0];
});
console.log(secretMessage.join(''));
or
或者
let animals = ['Hen', 'elephant', 'llama', 'leopard', 'ostrich', 'Whale', 'octopus', 'rabbit', 'lion', 'dog'];
let secretMessage = animals.map(animals => animals[0]);
console.log(secretMessage.join(''));
回答by Mukesh Suthar
.map()arrayfunction take callback function as an argument and return new array so that can use this method to get our result see the syntax below:
.map()array函数将回调函数作为参数并返回新数组,以便可以使用此方法获取我们的结果,请参见以下语法:
const animals = ['Hen', 'elephant', 'llama', 'leopard', 'ostrich', 'Whale', 'octopus', 'rabbit', 'lion', 'dog'];
const secretMessage = animals.map(animals => {
return animals[0];
})
console.log(secretMessage.join(''));
now when .map method is applied on the animals array its return only the first element of the each string because we use animals[0]which read only first element of each string so our output is HelloWorld
现在,当 .map 方法应用于动物数组时,它只返回每个字符串的第一个元素,因为我们使用animals[0]which 只读取每个字符串的第一个元素,所以我们的输出是HelloWorld
回答by Golden Panda
const animals = ['Hen', 'elephant', 'llama', 'leopard', 'ostrich', 'Whale', 'octopus', 'rabbit', 'lion', 'dog'];
const secretMessage = animals.map(animal => {
return animal[0];
})
console.log(secretMessage.join(''));

