Es6 functions iterators .map

Within the context of learning JavaScript.
I am learning higher order functions and trying to decode an array of words (creatures), and in turn take the first letter from each returned object to decode a secretMessage using the .map method. Is there a command that would produce similar results for a given string? Its completely outside the scope of what the lesson intends on teaching but I’m just overly curious I suppose.

const animals = ['Hen', 'elephant', 'llama', 'leopard', 'ostrich', 'Whale', 'octopus', 'rabbit', 'lion', 'dog'];

// Create the secretMessage array below
const secretMessage = animals.map(creature=>creature[0]);
console.log(secretMessage.join(''));

const bigNumbers = [100, 200, 300, 400, 500];

// Create the smallNumbers array below
const smallNumbers=bigNumbers.map(i=>i /100)
console.log(smallNumbers)


const randomString='this has got my brain on overload'
console.log(randomString)
console.log(randomString.map(i=>i[0]))

You could try the reduce method:

let secretMessage = animals.reduce((str, animal) => str += animal[0], '');
console.log(secretMessage) // "HelloWorld"

Your last console throws an error because you cannot use the map method on a string.

2 Likes