Passing arguments into map()











up vote
0
down vote

favorite












I am trying to use the built in map() function on an Array.from () that returns some elements using Puppeteer.



The below is the code:



let res = await page.evaluate(elementPath => {
return Array.from(document.querySelectorAll(elementPath), (cin, index) => {
return {
cs: `state is ${this.s}`, // returns state is undefined
cinemaIndex: index,
cinemaId: cin.getAttribute('data-id'),
cinemaName: cin.getAttribute('data-name'),
cinemaURL: cin.getAttribute('data-url'),
};
}, {
s: 'NSW'
});
}, `div[data-state=${cinemaState}] div.top-select-option a.eccheckbox`, cinemaState);


I am not able to assign cs with variable s or cinemaState.



Wondering if you have solution










share|improve this question




























    up vote
    0
    down vote

    favorite












    I am trying to use the built in map() function on an Array.from () that returns some elements using Puppeteer.



    The below is the code:



    let res = await page.evaluate(elementPath => {
    return Array.from(document.querySelectorAll(elementPath), (cin, index) => {
    return {
    cs: `state is ${this.s}`, // returns state is undefined
    cinemaIndex: index,
    cinemaId: cin.getAttribute('data-id'),
    cinemaName: cin.getAttribute('data-name'),
    cinemaURL: cin.getAttribute('data-url'),
    };
    }, {
    s: 'NSW'
    });
    }, `div[data-state=${cinemaState}] div.top-select-option a.eccheckbox`, cinemaState);


    I am not able to assign cs with variable s or cinemaState.



    Wondering if you have solution










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I am trying to use the built in map() function on an Array.from () that returns some elements using Puppeteer.



      The below is the code:



      let res = await page.evaluate(elementPath => {
      return Array.from(document.querySelectorAll(elementPath), (cin, index) => {
      return {
      cs: `state is ${this.s}`, // returns state is undefined
      cinemaIndex: index,
      cinemaId: cin.getAttribute('data-id'),
      cinemaName: cin.getAttribute('data-name'),
      cinemaURL: cin.getAttribute('data-url'),
      };
      }, {
      s: 'NSW'
      });
      }, `div[data-state=${cinemaState}] div.top-select-option a.eccheckbox`, cinemaState);


      I am not able to assign cs with variable s or cinemaState.



      Wondering if you have solution










      share|improve this question















      I am trying to use the built in map() function on an Array.from () that returns some elements using Puppeteer.



      The below is the code:



      let res = await page.evaluate(elementPath => {
      return Array.from(document.querySelectorAll(elementPath), (cin, index) => {
      return {
      cs: `state is ${this.s}`, // returns state is undefined
      cinemaIndex: index,
      cinemaId: cin.getAttribute('data-id'),
      cinemaName: cin.getAttribute('data-name'),
      cinemaURL: cin.getAttribute('data-url'),
      };
      }, {
      s: 'NSW'
      });
      }, `div[data-state=${cinemaState}] div.top-select-option a.eccheckbox`, cinemaState);


      I am not able to assign cs with variable s or cinemaState.



      Wondering if you have solution







      javascript node.js google-chrome-devtools puppeteer headless-browser






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 5 at 23:34

























      asked Nov 5 at 3:46









      Theepan Thevathasasn

      234




      234
























          3 Answers
          3






          active

          oldest

          votes

















          up vote
          0
          down vote













          [1,2,3,4].map(function(num, index,wholeArray){
          console.log(num,index,wholeArray,this.s);
          },{s:"nsw"})


          maps takes two argument callback and thisArg whatever u will pass at the second arg will be accessible bi this






          share|improve this answer




























            up vote
            0
            down vote













            You can assign s to the cinemaState property in your return statement using the following method:



            cinemaState: this.s,


            Additionally, Array.from() has a built-in map function, so you should call the map function from within Array.from() to avoid an intermediate array:



            Array.from(arrayLike, mapFn);     // good
            Array.from(arrayLike).map(mapFn); // bad


            Lastly, you may want to use quotes around cinemaState in your attribute selector within your template literal selector string:



            [data-state="${cinemaState}"] // good
            [data-state=${cinemaState}] // bad


            Your final code should look something like this:



            let res = await page.evaluate(elementPath => {
            return Array.from(document.querySelectorAll(elementPath), (cin, index) => {
            return {
            cinemaState: this.s,
            cinemaIndex: index,
            cinemaId: cin.getAttribute('data-id'),
            cinemaName: cin.getAttribute('data-name'),
            cinemaURL: cin.getAttribute('data-url'),
            };
            }, {
            s: 'NSW'
            });
            }, `div[data-state=${cinemaState}] div.top-select-option a.eccheckbox`, cinemaState);





            share|improve this answer























            • This is only if you are using the lamda syntax on a single line
              – C Smith
              Nov 5 at 19:01










            • @grant-miller, Not sure I am missing something. I will update the original question with my current code.. Basically cs: state is ${this.s}, // returns state is undefined
              – Theepan Thevathasasn
              Nov 5 at 23:29




















            up vote
            0
            down vote













            I am able to explain this. this is what has worked for me. I had to replace the arrow function to a traditional function



            let res = await page.evaluate(elementPath => {
            return Array.from(document.querySelectorAll(elementPath), function (cin, index) // changed from (cin, index) =>
            {
            return {
            cs: `state is ${this.s}`, // returns state is undefined
            cinemaIndex: index,
            cinemaId: cin.getAttribute('data-id'),
            cinemaName: cin.getAttribute('data-name'),
            cinemaURL: cin.getAttribute('data-url'),
            };
            }, {
            s: 'NSW'
            });
            }, `div[data-state=${cinemaState}] div.top-select-option a.eccheckbox`, cinemaState);





            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',
              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%2f53148045%2fpassing-arguments-into-map%23new-answer', 'question_page');
              }
              );

              Post as a guest
































              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              0
              down vote













              [1,2,3,4].map(function(num, index,wholeArray){
              console.log(num,index,wholeArray,this.s);
              },{s:"nsw"})


              maps takes two argument callback and thisArg whatever u will pass at the second arg will be accessible bi this






              share|improve this answer

























                up vote
                0
                down vote













                [1,2,3,4].map(function(num, index,wholeArray){
                console.log(num,index,wholeArray,this.s);
                },{s:"nsw"})


                maps takes two argument callback and thisArg whatever u will pass at the second arg will be accessible bi this






                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  [1,2,3,4].map(function(num, index,wholeArray){
                  console.log(num,index,wholeArray,this.s);
                  },{s:"nsw"})


                  maps takes two argument callback and thisArg whatever u will pass at the second arg will be accessible bi this






                  share|improve this answer












                  [1,2,3,4].map(function(num, index,wholeArray){
                  console.log(num,index,wholeArray,this.s);
                  },{s:"nsw"})


                  maps takes two argument callback and thisArg whatever u will pass at the second arg will be accessible bi this







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 5 at 3:59









                  Pranoy Sarkar

                  561311




                  561311
























                      up vote
                      0
                      down vote













                      You can assign s to the cinemaState property in your return statement using the following method:



                      cinemaState: this.s,


                      Additionally, Array.from() has a built-in map function, so you should call the map function from within Array.from() to avoid an intermediate array:



                      Array.from(arrayLike, mapFn);     // good
                      Array.from(arrayLike).map(mapFn); // bad


                      Lastly, you may want to use quotes around cinemaState in your attribute selector within your template literal selector string:



                      [data-state="${cinemaState}"] // good
                      [data-state=${cinemaState}] // bad


                      Your final code should look something like this:



                      let res = await page.evaluate(elementPath => {
                      return Array.from(document.querySelectorAll(elementPath), (cin, index) => {
                      return {
                      cinemaState: this.s,
                      cinemaIndex: index,
                      cinemaId: cin.getAttribute('data-id'),
                      cinemaName: cin.getAttribute('data-name'),
                      cinemaURL: cin.getAttribute('data-url'),
                      };
                      }, {
                      s: 'NSW'
                      });
                      }, `div[data-state=${cinemaState}] div.top-select-option a.eccheckbox`, cinemaState);





                      share|improve this answer























                      • This is only if you are using the lamda syntax on a single line
                        – C Smith
                        Nov 5 at 19:01










                      • @grant-miller, Not sure I am missing something. I will update the original question with my current code.. Basically cs: state is ${this.s}, // returns state is undefined
                        – Theepan Thevathasasn
                        Nov 5 at 23:29

















                      up vote
                      0
                      down vote













                      You can assign s to the cinemaState property in your return statement using the following method:



                      cinemaState: this.s,


                      Additionally, Array.from() has a built-in map function, so you should call the map function from within Array.from() to avoid an intermediate array:



                      Array.from(arrayLike, mapFn);     // good
                      Array.from(arrayLike).map(mapFn); // bad


                      Lastly, you may want to use quotes around cinemaState in your attribute selector within your template literal selector string:



                      [data-state="${cinemaState}"] // good
                      [data-state=${cinemaState}] // bad


                      Your final code should look something like this:



                      let res = await page.evaluate(elementPath => {
                      return Array.from(document.querySelectorAll(elementPath), (cin, index) => {
                      return {
                      cinemaState: this.s,
                      cinemaIndex: index,
                      cinemaId: cin.getAttribute('data-id'),
                      cinemaName: cin.getAttribute('data-name'),
                      cinemaURL: cin.getAttribute('data-url'),
                      };
                      }, {
                      s: 'NSW'
                      });
                      }, `div[data-state=${cinemaState}] div.top-select-option a.eccheckbox`, cinemaState);





                      share|improve this answer























                      • This is only if you are using the lamda syntax on a single line
                        – C Smith
                        Nov 5 at 19:01










                      • @grant-miller, Not sure I am missing something. I will update the original question with my current code.. Basically cs: state is ${this.s}, // returns state is undefined
                        – Theepan Thevathasasn
                        Nov 5 at 23:29















                      up vote
                      0
                      down vote










                      up vote
                      0
                      down vote









                      You can assign s to the cinemaState property in your return statement using the following method:



                      cinemaState: this.s,


                      Additionally, Array.from() has a built-in map function, so you should call the map function from within Array.from() to avoid an intermediate array:



                      Array.from(arrayLike, mapFn);     // good
                      Array.from(arrayLike).map(mapFn); // bad


                      Lastly, you may want to use quotes around cinemaState in your attribute selector within your template literal selector string:



                      [data-state="${cinemaState}"] // good
                      [data-state=${cinemaState}] // bad


                      Your final code should look something like this:



                      let res = await page.evaluate(elementPath => {
                      return Array.from(document.querySelectorAll(elementPath), (cin, index) => {
                      return {
                      cinemaState: this.s,
                      cinemaIndex: index,
                      cinemaId: cin.getAttribute('data-id'),
                      cinemaName: cin.getAttribute('data-name'),
                      cinemaURL: cin.getAttribute('data-url'),
                      };
                      }, {
                      s: 'NSW'
                      });
                      }, `div[data-state=${cinemaState}] div.top-select-option a.eccheckbox`, cinemaState);





                      share|improve this answer














                      You can assign s to the cinemaState property in your return statement using the following method:



                      cinemaState: this.s,


                      Additionally, Array.from() has a built-in map function, so you should call the map function from within Array.from() to avoid an intermediate array:



                      Array.from(arrayLike, mapFn);     // good
                      Array.from(arrayLike).map(mapFn); // bad


                      Lastly, you may want to use quotes around cinemaState in your attribute selector within your template literal selector string:



                      [data-state="${cinemaState}"] // good
                      [data-state=${cinemaState}] // bad


                      Your final code should look something like this:



                      let res = await page.evaluate(elementPath => {
                      return Array.from(document.querySelectorAll(elementPath), (cin, index) => {
                      return {
                      cinemaState: this.s,
                      cinemaIndex: index,
                      cinemaId: cin.getAttribute('data-id'),
                      cinemaName: cin.getAttribute('data-name'),
                      cinemaURL: cin.getAttribute('data-url'),
                      };
                      }, {
                      s: 'NSW'
                      });
                      }, `div[data-state=${cinemaState}] div.top-select-option a.eccheckbox`, cinemaState);






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Nov 5 at 19:16

























                      answered Nov 5 at 18:46









                      Grant Miller

                      4,681132346




                      4,681132346












                      • This is only if you are using the lamda syntax on a single line
                        – C Smith
                        Nov 5 at 19:01










                      • @grant-miller, Not sure I am missing something. I will update the original question with my current code.. Basically cs: state is ${this.s}, // returns state is undefined
                        – Theepan Thevathasasn
                        Nov 5 at 23:29




















                      • This is only if you are using the lamda syntax on a single line
                        – C Smith
                        Nov 5 at 19:01










                      • @grant-miller, Not sure I am missing something. I will update the original question with my current code.. Basically cs: state is ${this.s}, // returns state is undefined
                        – Theepan Thevathasasn
                        Nov 5 at 23:29


















                      This is only if you are using the lamda syntax on a single line
                      – C Smith
                      Nov 5 at 19:01




                      This is only if you are using the lamda syntax on a single line
                      – C Smith
                      Nov 5 at 19:01












                      @grant-miller, Not sure I am missing something. I will update the original question with my current code.. Basically cs: state is ${this.s}, // returns state is undefined
                      – Theepan Thevathasasn
                      Nov 5 at 23:29






                      @grant-miller, Not sure I am missing something. I will update the original question with my current code.. Basically cs: state is ${this.s}, // returns state is undefined
                      – Theepan Thevathasasn
                      Nov 5 at 23:29












                      up vote
                      0
                      down vote













                      I am able to explain this. this is what has worked for me. I had to replace the arrow function to a traditional function



                      let res = await page.evaluate(elementPath => {
                      return Array.from(document.querySelectorAll(elementPath), function (cin, index) // changed from (cin, index) =>
                      {
                      return {
                      cs: `state is ${this.s}`, // returns state is undefined
                      cinemaIndex: index,
                      cinemaId: cin.getAttribute('data-id'),
                      cinemaName: cin.getAttribute('data-name'),
                      cinemaURL: cin.getAttribute('data-url'),
                      };
                      }, {
                      s: 'NSW'
                      });
                      }, `div[data-state=${cinemaState}] div.top-select-option a.eccheckbox`, cinemaState);





                      share|improve this answer

























                        up vote
                        0
                        down vote













                        I am able to explain this. this is what has worked for me. I had to replace the arrow function to a traditional function



                        let res = await page.evaluate(elementPath => {
                        return Array.from(document.querySelectorAll(elementPath), function (cin, index) // changed from (cin, index) =>
                        {
                        return {
                        cs: `state is ${this.s}`, // returns state is undefined
                        cinemaIndex: index,
                        cinemaId: cin.getAttribute('data-id'),
                        cinemaName: cin.getAttribute('data-name'),
                        cinemaURL: cin.getAttribute('data-url'),
                        };
                        }, {
                        s: 'NSW'
                        });
                        }, `div[data-state=${cinemaState}] div.top-select-option a.eccheckbox`, cinemaState);





                        share|improve this answer























                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          I am able to explain this. this is what has worked for me. I had to replace the arrow function to a traditional function



                          let res = await page.evaluate(elementPath => {
                          return Array.from(document.querySelectorAll(elementPath), function (cin, index) // changed from (cin, index) =>
                          {
                          return {
                          cs: `state is ${this.s}`, // returns state is undefined
                          cinemaIndex: index,
                          cinemaId: cin.getAttribute('data-id'),
                          cinemaName: cin.getAttribute('data-name'),
                          cinemaURL: cin.getAttribute('data-url'),
                          };
                          }, {
                          s: 'NSW'
                          });
                          }, `div[data-state=${cinemaState}] div.top-select-option a.eccheckbox`, cinemaState);





                          share|improve this answer












                          I am able to explain this. this is what has worked for me. I had to replace the arrow function to a traditional function



                          let res = await page.evaluate(elementPath => {
                          return Array.from(document.querySelectorAll(elementPath), function (cin, index) // changed from (cin, index) =>
                          {
                          return {
                          cs: `state is ${this.s}`, // returns state is undefined
                          cinemaIndex: index,
                          cinemaId: cin.getAttribute('data-id'),
                          cinemaName: cin.getAttribute('data-name'),
                          cinemaURL: cin.getAttribute('data-url'),
                          };
                          }, {
                          s: 'NSW'
                          });
                          }, `div[data-state=${cinemaState}] div.top-select-option a.eccheckbox`, cinemaState);






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 7 at 9:09









                          Theepan Thevathasasn

                          234




                          234






























                               

                              draft saved


                              draft discarded



















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53148045%2fpassing-arguments-into-map%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest




















































































                              這個網誌中的熱門文章

                              Xamarin.form Move up view when keyboard appear

                              Post-Redirect-Get with Spring WebFlux and Thymeleaf

                              Anylogic : not able to use stopDelay()