[英]Getting the first characters of each string in an array in Javascript
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.嗨,通过这段代码,我想输出字符串HelloWorld
,它由动物数组中每个字符串/元素的第一个字符组成。 However, the output is instead HHHHHHHHHH
.但是,输出是HHHHHHHHHH
。 I don't know if the for loop is the problem here?我不知道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!提前致谢!
Map
is a for loop
in and of itself. Map
本身就是一个for loop
。
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"
The map method does the for loop for you. map 方法为您执行 for 循环。 So all you need in the function is animal[0]所以你在函数中需要的只是animal[0]
let secretMessage = animals.map(function(animal) {
return animal[0]
});
This will return every first character in every string in the array, animals.这将返回数组中每个字符串中的第一个字符,animals。 Everything else you have is right.你拥有的其他一切都是对的。
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(''));
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
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(''));
Just do this.就这样做。
let animals = [ 'Hen', 'elephant', 'llama', 'leopard', 'ostrich', 'Whale', 'octopus', 'rabbit', 'lion', 'dog' ]; console.log(animals.map(e => e[0]).join(""));
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(''));
.map()
array
function 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]
只读取每个字符串的第一个元素,所以我们的输出是HelloWorld
const animals = ['Hen', 'elephant', 'llama', 'leopard', 'ostrich', 'Whale', 'octopus', 'rabbit', 'lion', 'dog']; const secretMessage = animals.map(animal => { return animal[0]; }) console.log(secretMessage.join(''));
let animals = ['Hen', 'elephant', 'llama', 'leopard', 'ostrich', 'Whale', 'octopus', 'rabbit', 'lion', 'dog'];
animals.map(([animals]) => animals).join('')
Output: HelloWorld
输出: HelloWorld
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.