What is the purpose of spread syntax in this example? [duplicate]












-1
















This question already has an answer here:




  • Why does spread syntax convert my string into an array?

    5 answers




Here,we created Hashmap with keys and value.Then we use array.map to get our result. i.e If we input "cat",we will get output as "dog".I didnt get how spread syntax is used.



var rule = 
{
"c": "d",
"a": "o",
"t": "g",
"h": "a",
"e": "n",
"n": "t",
}

function output(str) {
return [...str].map(d => rule[d]).join('')
}

console.log(output('cat'))









share|improve this question













marked as duplicate by Mamun javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 25 '18 at 9:39


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • Could you not have tried it? Just run [...'cat'] in the console.

    – jonrsharpe
    Nov 25 '18 at 9:39
















-1
















This question already has an answer here:




  • Why does spread syntax convert my string into an array?

    5 answers




Here,we created Hashmap with keys and value.Then we use array.map to get our result. i.e If we input "cat",we will get output as "dog".I didnt get how spread syntax is used.



var rule = 
{
"c": "d",
"a": "o",
"t": "g",
"h": "a",
"e": "n",
"n": "t",
}

function output(str) {
return [...str].map(d => rule[d]).join('')
}

console.log(output('cat'))









share|improve this question













marked as duplicate by Mamun javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 25 '18 at 9:39


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • Could you not have tried it? Just run [...'cat'] in the console.

    – jonrsharpe
    Nov 25 '18 at 9:39














-1












-1








-1









This question already has an answer here:




  • Why does spread syntax convert my string into an array?

    5 answers




Here,we created Hashmap with keys and value.Then we use array.map to get our result. i.e If we input "cat",we will get output as "dog".I didnt get how spread syntax is used.



var rule = 
{
"c": "d",
"a": "o",
"t": "g",
"h": "a",
"e": "n",
"n": "t",
}

function output(str) {
return [...str].map(d => rule[d]).join('')
}

console.log(output('cat'))









share|improve this question















This question already has an answer here:




  • Why does spread syntax convert my string into an array?

    5 answers




Here,we created Hashmap with keys and value.Then we use array.map to get our result. i.e If we input "cat",we will get output as "dog".I didnt get how spread syntax is used.



var rule = 
{
"c": "d",
"a": "o",
"t": "g",
"h": "a",
"e": "n",
"n": "t",
}

function output(str) {
return [...str].map(d => rule[d]).join('')
}

console.log(output('cat'))




This question already has an answer here:




  • Why does spread syntax convert my string into an array?

    5 answers








javascript arrays






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 25 '18 at 9:32









Suman BasnetSuman Basnet

426




426




marked as duplicate by Mamun javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 25 '18 at 9:39


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Mamun javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 25 '18 at 9:39


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • Could you not have tried it? Just run [...'cat'] in the console.

    – jonrsharpe
    Nov 25 '18 at 9:39



















  • Could you not have tried it? Just run [...'cat'] in the console.

    – jonrsharpe
    Nov 25 '18 at 9:39

















Could you not have tried it? Just run [...'cat'] in the console.

– jonrsharpe
Nov 25 '18 at 9:39





Could you not have tried it? Just run [...'cat'] in the console.

– jonrsharpe
Nov 25 '18 at 9:39












5 Answers
5






active

oldest

votes


















0














Break it down:



[...str]



Gives: ['c', 'a', 't'];



In this case, the spread syntax breaks up the string into an array. Where each index represents a letter from the string.



.map(d => rule[d])



Converts the above array to:



['d', 'o', 'g']



As it goes through each value and uses the javascript object to get its value: (so 'c' maps to 'd' as rule['c'] = 'd', 'a' maps to 'o', etc...)



.join('')



Converts the above array to a string dog



Thus, doing:



console.log(output('cat'))



Gives: "dog"






share|improve this answer

































    0














    spread syntax on a string turns it into an array consisting of its characters. This works because strings are iterable collections. http://es6-features.org/#SpreadOperator






    share|improve this answer































      0














      In your case the spread operator spreads the string "cat" into an array of characters containing ['c','a','t']



      Then it iterates over this array to provide the output string matches the values. There you get the string "dog" as output!






      share|improve this answer































        0














        You would not be able to apply map to the string directly, as String does not have such a method. With the spread syntax you get an array of individual characters, and then map can be applied to that array.



        It is in fact better practice to use Array.from instead, because it does not create the intermediate array and still allows you to execute a function on each character (using the second argument):



        Array.from(str, d => rule[d]).join('')





        share|improve this answer































          0














          It takes an iterable and returns each item of the iterable by using the spread syntax ....



          In this case, a string is splitted to an array of characters.



          ['c', 'a', 't']


          The same result could be achieved, if you use Array.from, which has a built-in mapping parameter.






          var rule =  { c: "d", a: "o", t: "g", h: "a", e: "n", n: "t" }

          function output(str) {
          return Array.from(str, d => rule[d]).join('');
          }

          console.log(output('cat')) ;








          share|improve this answer
































            5 Answers
            5






            active

            oldest

            votes








            5 Answers
            5






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Break it down:



            [...str]



            Gives: ['c', 'a', 't'];



            In this case, the spread syntax breaks up the string into an array. Where each index represents a letter from the string.



            .map(d => rule[d])



            Converts the above array to:



            ['d', 'o', 'g']



            As it goes through each value and uses the javascript object to get its value: (so 'c' maps to 'd' as rule['c'] = 'd', 'a' maps to 'o', etc...)



            .join('')



            Converts the above array to a string dog



            Thus, doing:



            console.log(output('cat'))



            Gives: "dog"






            share|improve this answer






























              0














              Break it down:



              [...str]



              Gives: ['c', 'a', 't'];



              In this case, the spread syntax breaks up the string into an array. Where each index represents a letter from the string.



              .map(d => rule[d])



              Converts the above array to:



              ['d', 'o', 'g']



              As it goes through each value and uses the javascript object to get its value: (so 'c' maps to 'd' as rule['c'] = 'd', 'a' maps to 'o', etc...)



              .join('')



              Converts the above array to a string dog



              Thus, doing:



              console.log(output('cat'))



              Gives: "dog"






              share|improve this answer




























                0












                0








                0







                Break it down:



                [...str]



                Gives: ['c', 'a', 't'];



                In this case, the spread syntax breaks up the string into an array. Where each index represents a letter from the string.



                .map(d => rule[d])



                Converts the above array to:



                ['d', 'o', 'g']



                As it goes through each value and uses the javascript object to get its value: (so 'c' maps to 'd' as rule['c'] = 'd', 'a' maps to 'o', etc...)



                .join('')



                Converts the above array to a string dog



                Thus, doing:



                console.log(output('cat'))



                Gives: "dog"






                share|improve this answer















                Break it down:



                [...str]



                Gives: ['c', 'a', 't'];



                In this case, the spread syntax breaks up the string into an array. Where each index represents a letter from the string.



                .map(d => rule[d])



                Converts the above array to:



                ['d', 'o', 'g']



                As it goes through each value and uses the javascript object to get its value: (so 'c' maps to 'd' as rule['c'] = 'd', 'a' maps to 'o', etc...)



                .join('')



                Converts the above array to a string dog



                Thus, doing:



                console.log(output('cat'))



                Gives: "dog"







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 28 '18 at 9:05

























                answered Nov 25 '18 at 9:40









                Nick ParsonsNick Parsons

                8,7282824




                8,7282824

























                    0














                    spread syntax on a string turns it into an array consisting of its characters. This works because strings are iterable collections. http://es6-features.org/#SpreadOperator






                    share|improve this answer




























                      0














                      spread syntax on a string turns it into an array consisting of its characters. This works because strings are iterable collections. http://es6-features.org/#SpreadOperator






                      share|improve this answer


























                        0












                        0








                        0







                        spread syntax on a string turns it into an array consisting of its characters. This works because strings are iterable collections. http://es6-features.org/#SpreadOperator






                        share|improve this answer













                        spread syntax on a string turns it into an array consisting of its characters. This works because strings are iterable collections. http://es6-features.org/#SpreadOperator







                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Nov 25 '18 at 9:36









                        lluisrojasslluisrojass

                        11119




                        11119























                            0














                            In your case the spread operator spreads the string "cat" into an array of characters containing ['c','a','t']



                            Then it iterates over this array to provide the output string matches the values. There you get the string "dog" as output!






                            share|improve this answer




























                              0














                              In your case the spread operator spreads the string "cat" into an array of characters containing ['c','a','t']



                              Then it iterates over this array to provide the output string matches the values. There you get the string "dog" as output!






                              share|improve this answer


























                                0












                                0








                                0







                                In your case the spread operator spreads the string "cat" into an array of characters containing ['c','a','t']



                                Then it iterates over this array to provide the output string matches the values. There you get the string "dog" as output!






                                share|improve this answer













                                In your case the spread operator spreads the string "cat" into an array of characters containing ['c','a','t']



                                Then it iterates over this array to provide the output string matches the values. There you get the string "dog" as output!







                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Nov 25 '18 at 9:38









                                LuciferLucifer

                                304211




                                304211























                                    0














                                    You would not be able to apply map to the string directly, as String does not have such a method. With the spread syntax you get an array of individual characters, and then map can be applied to that array.



                                    It is in fact better practice to use Array.from instead, because it does not create the intermediate array and still allows you to execute a function on each character (using the second argument):



                                    Array.from(str, d => rule[d]).join('')





                                    share|improve this answer




























                                      0














                                      You would not be able to apply map to the string directly, as String does not have such a method. With the spread syntax you get an array of individual characters, and then map can be applied to that array.



                                      It is in fact better practice to use Array.from instead, because it does not create the intermediate array and still allows you to execute a function on each character (using the second argument):



                                      Array.from(str, d => rule[d]).join('')





                                      share|improve this answer


























                                        0












                                        0








                                        0







                                        You would not be able to apply map to the string directly, as String does not have such a method. With the spread syntax you get an array of individual characters, and then map can be applied to that array.



                                        It is in fact better practice to use Array.from instead, because it does not create the intermediate array and still allows you to execute a function on each character (using the second argument):



                                        Array.from(str, d => rule[d]).join('')





                                        share|improve this answer













                                        You would not be able to apply map to the string directly, as String does not have such a method. With the spread syntax you get an array of individual characters, and then map can be applied to that array.



                                        It is in fact better practice to use Array.from instead, because it does not create the intermediate array and still allows you to execute a function on each character (using the second argument):



                                        Array.from(str, d => rule[d]).join('')






                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered Nov 25 '18 at 9:39









                                        trincottrincot

                                        125k1588121




                                        125k1588121























                                            0














                                            It takes an iterable and returns each item of the iterable by using the spread syntax ....



                                            In this case, a string is splitted to an array of characters.



                                            ['c', 'a', 't']


                                            The same result could be achieved, if you use Array.from, which has a built-in mapping parameter.






                                            var rule =  { c: "d", a: "o", t: "g", h: "a", e: "n", n: "t" }

                                            function output(str) {
                                            return Array.from(str, d => rule[d]).join('');
                                            }

                                            console.log(output('cat')) ;








                                            share|improve this answer






























                                              0














                                              It takes an iterable and returns each item of the iterable by using the spread syntax ....



                                              In this case, a string is splitted to an array of characters.



                                              ['c', 'a', 't']


                                              The same result could be achieved, if you use Array.from, which has a built-in mapping parameter.






                                              var rule =  { c: "d", a: "o", t: "g", h: "a", e: "n", n: "t" }

                                              function output(str) {
                                              return Array.from(str, d => rule[d]).join('');
                                              }

                                              console.log(output('cat')) ;








                                              share|improve this answer




























                                                0












                                                0








                                                0







                                                It takes an iterable and returns each item of the iterable by using the spread syntax ....



                                                In this case, a string is splitted to an array of characters.



                                                ['c', 'a', 't']


                                                The same result could be achieved, if you use Array.from, which has a built-in mapping parameter.






                                                var rule =  { c: "d", a: "o", t: "g", h: "a", e: "n", n: "t" }

                                                function output(str) {
                                                return Array.from(str, d => rule[d]).join('');
                                                }

                                                console.log(output('cat')) ;








                                                share|improve this answer















                                                It takes an iterable and returns each item of the iterable by using the spread syntax ....



                                                In this case, a string is splitted to an array of characters.



                                                ['c', 'a', 't']


                                                The same result could be achieved, if you use Array.from, which has a built-in mapping parameter.






                                                var rule =  { c: "d", a: "o", t: "g", h: "a", e: "n", n: "t" }

                                                function output(str) {
                                                return Array.from(str, d => rule[d]).join('');
                                                }

                                                console.log(output('cat')) ;








                                                var rule =  { c: "d", a: "o", t: "g", h: "a", e: "n", n: "t" }

                                                function output(str) {
                                                return Array.from(str, d => rule[d]).join('');
                                                }

                                                console.log(output('cat')) ;





                                                var rule =  { c: "d", a: "o", t: "g", h: "a", e: "n", n: "t" }

                                                function output(str) {
                                                return Array.from(str, d => rule[d]).join('');
                                                }

                                                console.log(output('cat')) ;






                                                share|improve this answer














                                                share|improve this answer



                                                share|improve this answer








                                                edited Nov 28 '18 at 9:07









                                                Nick Parsons

                                                8,7282824




                                                8,7282824










                                                answered Nov 25 '18 at 9:38









                                                Nina ScholzNina Scholz

                                                187k1596172




                                                187k1596172















                                                    Popular posts from this blog

                                                    404 Error Contact Form 7 ajax form submitting

                                                    How to know if a Active Directory user can login interactively

                                                    Refactoring coordinates for Minecraft Pi buildings written in Python