replace filter and map with reduce es6












1















I'm trying to avoid chaining with filter and map, but why my reduce returned undefined?



const data = [{
value: "moto",
passed: true
},{
value: "boat",
passed: false
}, {
value: "car",
passed: true
}]

// expected ['moto', 'car']
// using filter and map
data.filter(obj => obj.passed).map(obj => obj.value)


What's wrong?
data.reduce((accum, obj) => obj.checked && [...accum, obj.value], )










share|improve this question


















  • 1





    It is "passed" not "checked" on your data.

    – Alex G
    Nov 19 '18 at 11:42






  • 1





    You need a ternary operator in your reduce callback, not &&. Imagine what you return when obj.passed is false... it should be accum, but instead you return false.

    – trincot
    Nov 19 '18 at 11:43








  • 1





    What's wrong with chaining filter and map?

    – Denis Frezzato
    Nov 19 '18 at 11:44











  • @Denis - you run similar loop 2 times instead of one. So it might, for example, take 4sec instead of 2sec.

    – pbialy
    Nov 19 '18 at 11:45











  • The solution with reduce would be this: data.reduce((accum, obj) => obj.passed ? [...accum, obj.value] : accum, ). But there is nothing wrong with using filter and map.

    – Carsten Führmann
    Nov 19 '18 at 11:55
















1















I'm trying to avoid chaining with filter and map, but why my reduce returned undefined?



const data = [{
value: "moto",
passed: true
},{
value: "boat",
passed: false
}, {
value: "car",
passed: true
}]

// expected ['moto', 'car']
// using filter and map
data.filter(obj => obj.passed).map(obj => obj.value)


What's wrong?
data.reduce((accum, obj) => obj.checked && [...accum, obj.value], )










share|improve this question


















  • 1





    It is "passed" not "checked" on your data.

    – Alex G
    Nov 19 '18 at 11:42






  • 1





    You need a ternary operator in your reduce callback, not &&. Imagine what you return when obj.passed is false... it should be accum, but instead you return false.

    – trincot
    Nov 19 '18 at 11:43








  • 1





    What's wrong with chaining filter and map?

    – Denis Frezzato
    Nov 19 '18 at 11:44











  • @Denis - you run similar loop 2 times instead of one. So it might, for example, take 4sec instead of 2sec.

    – pbialy
    Nov 19 '18 at 11:45











  • The solution with reduce would be this: data.reduce((accum, obj) => obj.passed ? [...accum, obj.value] : accum, ). But there is nothing wrong with using filter and map.

    – Carsten Führmann
    Nov 19 '18 at 11:55














1












1








1


1






I'm trying to avoid chaining with filter and map, but why my reduce returned undefined?



const data = [{
value: "moto",
passed: true
},{
value: "boat",
passed: false
}, {
value: "car",
passed: true
}]

// expected ['moto', 'car']
// using filter and map
data.filter(obj => obj.passed).map(obj => obj.value)


What's wrong?
data.reduce((accum, obj) => obj.checked && [...accum, obj.value], )










share|improve this question














I'm trying to avoid chaining with filter and map, but why my reduce returned undefined?



const data = [{
value: "moto",
passed: true
},{
value: "boat",
passed: false
}, {
value: "car",
passed: true
}]

// expected ['moto', 'car']
// using filter and map
data.filter(obj => obj.passed).map(obj => obj.value)


What's wrong?
data.reduce((accum, obj) => obj.checked && [...accum, obj.value], )







javascript ecmascript-6






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 '18 at 11:39









MichealMicheal

213




213








  • 1





    It is "passed" not "checked" on your data.

    – Alex G
    Nov 19 '18 at 11:42






  • 1





    You need a ternary operator in your reduce callback, not &&. Imagine what you return when obj.passed is false... it should be accum, but instead you return false.

    – trincot
    Nov 19 '18 at 11:43








  • 1





    What's wrong with chaining filter and map?

    – Denis Frezzato
    Nov 19 '18 at 11:44











  • @Denis - you run similar loop 2 times instead of one. So it might, for example, take 4sec instead of 2sec.

    – pbialy
    Nov 19 '18 at 11:45











  • The solution with reduce would be this: data.reduce((accum, obj) => obj.passed ? [...accum, obj.value] : accum, ). But there is nothing wrong with using filter and map.

    – Carsten Führmann
    Nov 19 '18 at 11:55














  • 1





    It is "passed" not "checked" on your data.

    – Alex G
    Nov 19 '18 at 11:42






  • 1





    You need a ternary operator in your reduce callback, not &&. Imagine what you return when obj.passed is false... it should be accum, but instead you return false.

    – trincot
    Nov 19 '18 at 11:43








  • 1





    What's wrong with chaining filter and map?

    – Denis Frezzato
    Nov 19 '18 at 11:44











  • @Denis - you run similar loop 2 times instead of one. So it might, for example, take 4sec instead of 2sec.

    – pbialy
    Nov 19 '18 at 11:45











  • The solution with reduce would be this: data.reduce((accum, obj) => obj.passed ? [...accum, obj.value] : accum, ). But there is nothing wrong with using filter and map.

    – Carsten Führmann
    Nov 19 '18 at 11:55








1




1





It is "passed" not "checked" on your data.

– Alex G
Nov 19 '18 at 11:42





It is "passed" not "checked" on your data.

– Alex G
Nov 19 '18 at 11:42




1




1





You need a ternary operator in your reduce callback, not &&. Imagine what you return when obj.passed is false... it should be accum, but instead you return false.

– trincot
Nov 19 '18 at 11:43







You need a ternary operator in your reduce callback, not &&. Imagine what you return when obj.passed is false... it should be accum, but instead you return false.

– trincot
Nov 19 '18 at 11:43






1




1





What's wrong with chaining filter and map?

– Denis Frezzato
Nov 19 '18 at 11:44





What's wrong with chaining filter and map?

– Denis Frezzato
Nov 19 '18 at 11:44













@Denis - you run similar loop 2 times instead of one. So it might, for example, take 4sec instead of 2sec.

– pbialy
Nov 19 '18 at 11:45





@Denis - you run similar loop 2 times instead of one. So it might, for example, take 4sec instead of 2sec.

– pbialy
Nov 19 '18 at 11:45













The solution with reduce would be this: data.reduce((accum, obj) => obj.passed ? [...accum, obj.value] : accum, ). But there is nothing wrong with using filter and map.

– Carsten Führmann
Nov 19 '18 at 11:55





The solution with reduce would be this: data.reduce((accum, obj) => obj.passed ? [...accum, obj.value] : accum, ). But there is nothing wrong with using filter and map.

– Carsten Führmann
Nov 19 '18 at 11:55












3 Answers
3






active

oldest

votes


















1














 (accum, obj) => obj.checked && [...accum, obj.value]


does not return the filtered list, in case the object is not checked.



(accum, obj) => {
if (obj.checked) accum.push(obj.value)
return accum;
}


as reducer function will do that.



or to keep it as a oneliner, to not maintain readability:



(accum, obj) => obj.checked ? [...accum, obj.value] : accum;





share|improve this answer































    2














    You can do:






    const data = [{value: "moto",passed: true}, {value: "boat",passed: false}, {value: "car",passed: true}];
    const result = data.reduce((a, c) => c.passed ? (a.push(c.value), a) : a, );

    console.log(result);








    share|improve this answer



















    • 1





      +1 for using (a.push(c.value), a), that removes the immutable nature of [...a, c.value], but since that is a simple reducer its ok in favor of performance.

      – philipp
      Nov 19 '18 at 12:06








    • 1





      why use push? can't it be using spread?

      – Micheal
      Nov 19 '18 at 13:01



















    0














    It's because you are not returning anything if obj.passed = false. Updated below. Pls check






    const data = [{
    value: "moto",
    passed: true
    },{
    value: "boat",
    passed: false
    }, {
    value: "car",
    passed: true
    }]

    console.log(data.reduce((accum, obj) =>
    obj.passed
    ? accum.concat(obj.value)
    : accum
    , ))








    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%2f53373863%2freplace-filter-and-map-with-reduce-es6%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














       (accum, obj) => obj.checked && [...accum, obj.value]


      does not return the filtered list, in case the object is not checked.



      (accum, obj) => {
      if (obj.checked) accum.push(obj.value)
      return accum;
      }


      as reducer function will do that.



      or to keep it as a oneliner, to not maintain readability:



      (accum, obj) => obj.checked ? [...accum, obj.value] : accum;





      share|improve this answer




























        1














         (accum, obj) => obj.checked && [...accum, obj.value]


        does not return the filtered list, in case the object is not checked.



        (accum, obj) => {
        if (obj.checked) accum.push(obj.value)
        return accum;
        }


        as reducer function will do that.



        or to keep it as a oneliner, to not maintain readability:



        (accum, obj) => obj.checked ? [...accum, obj.value] : accum;





        share|improve this answer


























          1












          1








          1







           (accum, obj) => obj.checked && [...accum, obj.value]


          does not return the filtered list, in case the object is not checked.



          (accum, obj) => {
          if (obj.checked) accum.push(obj.value)
          return accum;
          }


          as reducer function will do that.



          or to keep it as a oneliner, to not maintain readability:



          (accum, obj) => obj.checked ? [...accum, obj.value] : accum;





          share|improve this answer













           (accum, obj) => obj.checked && [...accum, obj.value]


          does not return the filtered list, in case the object is not checked.



          (accum, obj) => {
          if (obj.checked) accum.push(obj.value)
          return accum;
          }


          as reducer function will do that.



          or to keep it as a oneliner, to not maintain readability:



          (accum, obj) => obj.checked ? [...accum, obj.value] : accum;






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 19 '18 at 11:43









          philippphilipp

          8,76653570




          8,76653570

























              2














              You can do:






              const data = [{value: "moto",passed: true}, {value: "boat",passed: false}, {value: "car",passed: true}];
              const result = data.reduce((a, c) => c.passed ? (a.push(c.value), a) : a, );

              console.log(result);








              share|improve this answer



















              • 1





                +1 for using (a.push(c.value), a), that removes the immutable nature of [...a, c.value], but since that is a simple reducer its ok in favor of performance.

                – philipp
                Nov 19 '18 at 12:06








              • 1





                why use push? can't it be using spread?

                – Micheal
                Nov 19 '18 at 13:01
















              2














              You can do:






              const data = [{value: "moto",passed: true}, {value: "boat",passed: false}, {value: "car",passed: true}];
              const result = data.reduce((a, c) => c.passed ? (a.push(c.value), a) : a, );

              console.log(result);








              share|improve this answer



















              • 1





                +1 for using (a.push(c.value), a), that removes the immutable nature of [...a, c.value], but since that is a simple reducer its ok in favor of performance.

                – philipp
                Nov 19 '18 at 12:06








              • 1





                why use push? can't it be using spread?

                – Micheal
                Nov 19 '18 at 13:01














              2












              2








              2







              You can do:






              const data = [{value: "moto",passed: true}, {value: "boat",passed: false}, {value: "car",passed: true}];
              const result = data.reduce((a, c) => c.passed ? (a.push(c.value), a) : a, );

              console.log(result);








              share|improve this answer













              You can do:






              const data = [{value: "moto",passed: true}, {value: "boat",passed: false}, {value: "car",passed: true}];
              const result = data.reduce((a, c) => c.passed ? (a.push(c.value), a) : a, );

              console.log(result);








              const data = [{value: "moto",passed: true}, {value: "boat",passed: false}, {value: "car",passed: true}];
              const result = data.reduce((a, c) => c.passed ? (a.push(c.value), a) : a, );

              console.log(result);





              const data = [{value: "moto",passed: true}, {value: "boat",passed: false}, {value: "car",passed: true}];
              const result = data.reduce((a, c) => c.passed ? (a.push(c.value), a) : a, );

              console.log(result);






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 19 '18 at 11:49









              Yosvel QuinteroYosvel Quintero

              11.2k42430




              11.2k42430








              • 1





                +1 for using (a.push(c.value), a), that removes the immutable nature of [...a, c.value], but since that is a simple reducer its ok in favor of performance.

                – philipp
                Nov 19 '18 at 12:06








              • 1





                why use push? can't it be using spread?

                – Micheal
                Nov 19 '18 at 13:01














              • 1





                +1 for using (a.push(c.value), a), that removes the immutable nature of [...a, c.value], but since that is a simple reducer its ok in favor of performance.

                – philipp
                Nov 19 '18 at 12:06








              • 1





                why use push? can't it be using spread?

                – Micheal
                Nov 19 '18 at 13:01








              1




              1





              +1 for using (a.push(c.value), a), that removes the immutable nature of [...a, c.value], but since that is a simple reducer its ok in favor of performance.

              – philipp
              Nov 19 '18 at 12:06







              +1 for using (a.push(c.value), a), that removes the immutable nature of [...a, c.value], but since that is a simple reducer its ok in favor of performance.

              – philipp
              Nov 19 '18 at 12:06






              1




              1





              why use push? can't it be using spread?

              – Micheal
              Nov 19 '18 at 13:01





              why use push? can't it be using spread?

              – Micheal
              Nov 19 '18 at 13:01











              0














              It's because you are not returning anything if obj.passed = false. Updated below. Pls check






              const data = [{
              value: "moto",
              passed: true
              },{
              value: "boat",
              passed: false
              }, {
              value: "car",
              passed: true
              }]

              console.log(data.reduce((accum, obj) =>
              obj.passed
              ? accum.concat(obj.value)
              : accum
              , ))








              share|improve this answer




























                0














                It's because you are not returning anything if obj.passed = false. Updated below. Pls check






                const data = [{
                value: "moto",
                passed: true
                },{
                value: "boat",
                passed: false
                }, {
                value: "car",
                passed: true
                }]

                console.log(data.reduce((accum, obj) =>
                obj.passed
                ? accum.concat(obj.value)
                : accum
                , ))








                share|improve this answer


























                  0












                  0








                  0







                  It's because you are not returning anything if obj.passed = false. Updated below. Pls check






                  const data = [{
                  value: "moto",
                  passed: true
                  },{
                  value: "boat",
                  passed: false
                  }, {
                  value: "car",
                  passed: true
                  }]

                  console.log(data.reduce((accum, obj) =>
                  obj.passed
                  ? accum.concat(obj.value)
                  : accum
                  , ))








                  share|improve this answer













                  It's because you are not returning anything if obj.passed = false. Updated below. Pls check






                  const data = [{
                  value: "moto",
                  passed: true
                  },{
                  value: "boat",
                  passed: false
                  }, {
                  value: "car",
                  passed: true
                  }]

                  console.log(data.reduce((accum, obj) =>
                  obj.passed
                  ? accum.concat(obj.value)
                  : accum
                  , ))








                  const data = [{
                  value: "moto",
                  passed: true
                  },{
                  value: "boat",
                  passed: false
                  }, {
                  value: "car",
                  passed: true
                  }]

                  console.log(data.reduce((accum, obj) =>
                  obj.passed
                  ? accum.concat(obj.value)
                  : accum
                  , ))





                  const data = [{
                  value: "moto",
                  passed: true
                  },{
                  value: "boat",
                  passed: false
                  }, {
                  value: "car",
                  passed: true
                  }]

                  console.log(data.reduce((accum, obj) =>
                  obj.passed
                  ? accum.concat(obj.value)
                  : accum
                  , ))






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 19 '18 at 11:46









                  Nitish NarangNitish Narang

                  2,9401815




                  2,9401815






























                      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%2f53373863%2freplace-filter-and-map-with-reduce-es6%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







                      這個網誌中的熱門文章

                      Xamarin.form Move up view when keyboard appear

                      Post-Redirect-Get with Spring WebFlux and Thymeleaf

                      Anylogic : not able to use stopDelay()