Getting the first characters of each string in an array in Javascript





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















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?



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!










share|improve this question

























  • Possible duplicate of How to read first character of string inside an array without using string.charAt()?

    – shawon191
    Oct 31 '17 at 12:49











  • animals.map(function(s) { return s[0]; }).join("");

    – Yogesh Chuahan
    Oct 31 '17 at 13:01


















1















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?



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!










share|improve this question

























  • Possible duplicate of How to read first character of string inside an array without using string.charAt()?

    – shawon191
    Oct 31 '17 at 12:49











  • animals.map(function(s) { return s[0]; }).join("");

    – Yogesh Chuahan
    Oct 31 '17 at 13:01














1












1








1


1






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?



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!










share|improve this question
















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?



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!







javascript arrays loops






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 31 '17 at 13:07









Sandeep Sukhija

8311021




8311021










asked Oct 31 '17 at 12:43









jack coltranejack coltrane

1816




1816













  • Possible duplicate of How to read first character of string inside an array without using string.charAt()?

    – shawon191
    Oct 31 '17 at 12:49











  • animals.map(function(s) { return s[0]; }).join("");

    – Yogesh Chuahan
    Oct 31 '17 at 13:01



















  • Possible duplicate of How to read first character of string inside an array without using string.charAt()?

    – shawon191
    Oct 31 '17 at 12:49











  • animals.map(function(s) { return s[0]; }).join("");

    – Yogesh Chuahan
    Oct 31 '17 at 13:01

















Possible duplicate of How to read first character of string inside an array without using string.charAt()?

– shawon191
Oct 31 '17 at 12:49





Possible duplicate of How to read first character of string inside an array without using string.charAt()?

– shawon191
Oct 31 '17 at 12:49













animals.map(function(s) { return s[0]; }).join("");

– Yogesh Chuahan
Oct 31 '17 at 13:01





animals.map(function(s) { return s[0]; }).join("");

– Yogesh Chuahan
Oct 31 '17 at 13:01












8 Answers
8






active

oldest

votes


















3














Map is a for loop in 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"








share|improve this answer

































    3














    The map method does the for loop for you. So all you need in the function is 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.






    share|improve this answer































      1














      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(''));








      share|improve this answer































        0














        The error here is that you have a loop inside the map function, which already loops the array.



        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





        share|improve this answer































          0














          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(''));





          share|improve this answer































            0














            Just do this.






            let animals = [
            'Hen', 'elephant', 'llama',
            'leopard', 'ostrich', 'Whale',
            'octopus', 'rabbit', 'lion', 'dog'
            ];
            console.log(animals.map(e => e[0]).join(""));








            share|improve this answer





















            • 1





              For ES5 or less use this: animals.map(function(s) { return s[0]; }).join(""); For ES6: () => { }

              – Yogesh Chuahan
              Oct 31 '17 at 13:02



















            0














            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(''));





            share|improve this answer































              0














              .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:



              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






              share|improve this answer


























                Your Answer






                StackExchange.ifUsing("editor", function () {
                StackExchange.using("externalEditor", function () {
                StackExchange.using("snippets", function () {
                StackExchange.snippets.init();
                });
                });
                }, "code-snippets");

                StackExchange.ready(function() {
                var channelOptions = {
                tags: "".split(" "),
                id: "1"
                };
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function() {
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled) {
                StackExchange.using("snippets", function() {
                createEditor();
                });
                }
                else {
                createEditor();
                }
                });

                function createEditor() {
                StackExchange.prepareEditor({
                heartbeatType: 'answer',
                autoActivateHeartbeat: false,
                convertImagesToLinks: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                imageUploader: {
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                },
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                });


                }
                });














                draft saved

                draft discarded


















                StackExchange.ready(
                function () {
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f47035752%2fgetting-the-first-characters-of-each-string-in-an-array-in-javascript%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                8 Answers
                8






                active

                oldest

                votes








                8 Answers
                8






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                3














                Map is a for loop in 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"








                share|improve this answer






























                  3














                  Map is a for loop in 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"








                  share|improve this answer




























                    3












                    3








                    3







                    Map is a for loop in 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"








                    share|improve this answer















                    Map is a for loop in 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"








                    // 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"





                    // 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"





                    // 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"





                    // 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"






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited May 10 '18 at 7:53

























                    answered Oct 31 '17 at 12:47









                    Arman CharanArman Charan

                    3,29821121




                    3,29821121

























                        3














                        The map method does the for loop for you. So all you need in the function is 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.






                        share|improve this answer




























                          3














                          The map method does the for loop for you. So all you need in the function is 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.






                          share|improve this answer


























                            3












                            3








                            3







                            The map method does the for loop for you. So all you need in the function is 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.






                            share|improve this answer













                            The map method does the for loop for you. So all you need in the function is 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.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Oct 31 '17 at 14:19









                            Joseph CarrickJoseph Carrick

                            312




                            312























                                1














                                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(''));








                                share|improve this answer




























                                  1














                                  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(''));








                                  share|improve this answer


























                                    1












                                    1








                                    1







                                    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(''));








                                    share|improve this answer













                                    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(''));








                                    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(''));





                                    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(''));






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Oct 31 '17 at 12:46









                                    dfsqdfsq

                                    168k21184213




                                    168k21184213























                                        0














                                        The error here is that you have a loop inside the map function, which already loops the array.



                                        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





                                        share|improve this answer




























                                          0














                                          The error here is that you have a loop inside the map function, which already loops the array.



                                          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





                                          share|improve this answer


























                                            0












                                            0








                                            0







                                            The error here is that you have a loop inside the map function, which already loops the array.



                                            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





                                            share|improve this answer













                                            The error here is that you have a loop inside the map function, which already loops the array.



                                            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






                                            share|improve this answer












                                            share|improve this answer



                                            share|improve this answer










                                            answered Oct 31 '17 at 12:46









                                            Luca De NardiLuca De Nardi

                                            1,500624




                                            1,500624























                                                0














                                                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(''));





                                                share|improve this answer




























                                                  0














                                                  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(''));





                                                  share|improve this answer


























                                                    0












                                                    0








                                                    0







                                                    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(''));





                                                    share|improve this answer













                                                    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(''));






                                                    share|improve this answer












                                                    share|improve this answer



                                                    share|improve this answer










                                                    answered Oct 31 '17 at 12:46









                                                    Kenji MukaiKenji Mukai

                                                    60148




                                                    60148























                                                        0














                                                        Just do this.






                                                        let animals = [
                                                        'Hen', 'elephant', 'llama',
                                                        'leopard', 'ostrich', 'Whale',
                                                        'octopus', 'rabbit', 'lion', 'dog'
                                                        ];
                                                        console.log(animals.map(e => e[0]).join(""));








                                                        share|improve this answer





















                                                        • 1





                                                          For ES5 or less use this: animals.map(function(s) { return s[0]; }).join(""); For ES6: () => { }

                                                          – Yogesh Chuahan
                                                          Oct 31 '17 at 13:02
















                                                        0














                                                        Just do this.






                                                        let animals = [
                                                        'Hen', 'elephant', 'llama',
                                                        'leopard', 'ostrich', 'Whale',
                                                        'octopus', 'rabbit', 'lion', 'dog'
                                                        ];
                                                        console.log(animals.map(e => e[0]).join(""));








                                                        share|improve this answer





















                                                        • 1





                                                          For ES5 or less use this: animals.map(function(s) { return s[0]; }).join(""); For ES6: () => { }

                                                          – Yogesh Chuahan
                                                          Oct 31 '17 at 13:02














                                                        0












                                                        0








                                                        0







                                                        Just do this.






                                                        let animals = [
                                                        'Hen', 'elephant', 'llama',
                                                        'leopard', 'ostrich', 'Whale',
                                                        'octopus', 'rabbit', 'lion', 'dog'
                                                        ];
                                                        console.log(animals.map(e => e[0]).join(""));








                                                        share|improve this answer















                                                        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'
                                                        ];
                                                        console.log(animals.map(e => e[0]).join(""));





                                                        let animals = [
                                                        'Hen', 'elephant', 'llama',
                                                        'leopard', 'ostrich', 'Whale',
                                                        'octopus', 'rabbit', 'lion', 'dog'
                                                        ];
                                                        console.log(animals.map(e => e[0]).join(""));






                                                        share|improve this answer














                                                        share|improve this answer



                                                        share|improve this answer








                                                        edited Oct 31 '17 at 12:52

























                                                        answered Oct 31 '17 at 12:46









                                                        marmeladzemarmeladze

                                                        4,39131433




                                                        4,39131433








                                                        • 1





                                                          For ES5 or less use this: animals.map(function(s) { return s[0]; }).join(""); For ES6: () => { }

                                                          – Yogesh Chuahan
                                                          Oct 31 '17 at 13:02














                                                        • 1





                                                          For ES5 or less use this: animals.map(function(s) { return s[0]; }).join(""); For ES6: () => { }

                                                          – Yogesh Chuahan
                                                          Oct 31 '17 at 13:02








                                                        1




                                                        1





                                                        For ES5 or less use this: animals.map(function(s) { return s[0]; }).join(""); For ES6: () => { }

                                                        – Yogesh Chuahan
                                                        Oct 31 '17 at 13:02





                                                        For ES5 or less use this: animals.map(function(s) { return s[0]; }).join(""); For ES6: () => { }

                                                        – Yogesh Chuahan
                                                        Oct 31 '17 at 13:02











                                                        0














                                                        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(''));





                                                        share|improve this answer




























                                                          0














                                                          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(''));





                                                          share|improve this answer


























                                                            0












                                                            0








                                                            0







                                                            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(''));





                                                            share|improve this answer













                                                            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(''));






                                                            share|improve this answer












                                                            share|improve this answer



                                                            share|improve this answer










                                                            answered Mar 30 '18 at 15:08









                                                            Kenny HeenatigalaKenny Heenatigala

                                                            12




                                                            12























                                                                0














                                                                .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:



                                                                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






                                                                share|improve this answer






























                                                                  0














                                                                  .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:



                                                                  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






                                                                  share|improve this answer




























                                                                    0












                                                                    0








                                                                    0







                                                                    .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:



                                                                    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






                                                                    share|improve this answer















                                                                    .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:



                                                                    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







                                                                    share|improve this answer














                                                                    share|improve this answer



                                                                    share|improve this answer








                                                                    edited Nov 23 '18 at 6:13









                                                                    Ram Ghadiyaram

                                                                    17.6k84980




                                                                    17.6k84980










                                                                    answered Nov 23 '18 at 6:07









                                                                    Mukesh SutharMukesh Suthar

                                                                    11




                                                                    11






























                                                                        draft saved

                                                                        draft discarded




















































                                                                        Thanks for contributing an answer to Stack Overflow!


                                                                        • Please be sure to answer the question. Provide details and share your research!

                                                                        But avoid



                                                                        • Asking for help, clarification, or responding to other answers.

                                                                        • Making statements based on opinion; back them up with references or personal experience.


                                                                        To learn more, see our tips on writing great answers.




                                                                        draft saved


                                                                        draft discarded














                                                                        StackExchange.ready(
                                                                        function () {
                                                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f47035752%2fgetting-the-first-characters-of-each-string-in-an-array-in-javascript%23new-answer', 'question_page');
                                                                        }
                                                                        );

                                                                        Post as a guest















                                                                        Required, but never shown





















































                                                                        Required, but never shown














                                                                        Required, but never shown












                                                                        Required, but never shown







                                                                        Required, but never shown

































                                                                        Required, but never shown














                                                                        Required, but never shown












                                                                        Required, but never shown







                                                                        Required, but never shown







                                                                        Popular posts from this blog

                                                                        How to change which sound is reproduced for terminal bell?

                                                                        Can I use Tabulator js library in my java Spring + Thymeleaf project?

                                                                        Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents