Loading ...
Sorry, an error occurred while loading the content.

Array.prototype.reduce

Expand Messages
  • rodobrist
    Hi guys, I was coding and I noticed that I often make the same error from using reduce. e.g. var options = [ nocaps , evil , node , browser ],
    Message 1 of 5 , Nov 25, 2012
    • 0 Attachment
      Hi guys,

      I was coding and I noticed that I often make the same error from using reduce.

      e.g.

      var options = ['nocaps', 'evil', 'node', 'browser'],
      optionsAsObject = options.reduce(function (options, item) {
      options[item] = true;
      }, {});

      // When it should be:
      var options = ['nocaps', 'evil', 'node', 'browser'],
      optionsAsObject = options.reduce(function (options, item) {
      options[item] = true;
      return options;
      }, {});

      Functions have a default/implied return value of undefined, this means if the array has more than one item this will throw an error.

      I'm suggesting that if the reduce method is invoked with a non primative second argument and function's first argument has an assignment to its property, there must be an explicit return value.

      e.g.

      options.reduce(function (options, item) {
      // first argument is modified
      options[item] = true;

      // Missing return value
      }, {}); // reduce is invoked with non primative second argument

      Feedback guys?
    • douglascrockford
      ... I think the error may be that you are using the wrong method: reduce when you should forEach. I am not sure how to test for that.
      Message 2 of 5 , Nov 26, 2012
      • 0 Attachment
        --- In jslint_com@yahoogroups.com, "rodobrist" <roderickobrist@...> wrote:
        >
        > Hi guys,
        >
        > I was coding and I noticed that I often make the same error from using reduce.
        >
        > e.g.
        >
        > var options = ['nocaps', 'evil', 'node', 'browser'],
        > optionsAsObject = options.reduce(function (options, item) {
        > options[item] = true;
        > }, {});
        >
        > // When it should be:
        > var options = ['nocaps', 'evil', 'node', 'browser'],
        > optionsAsObject = options.reduce(function (options, item) {
        > options[item] = true;
        > return options;
        > }, {});
        >
        > Functions have a default/implied return value of undefined, this means if the array has more than one item this will throw an error.
        >
        > I'm suggesting that if the reduce method is invoked with a non primative second argument and function's first argument has an assignment to its property, there must be an explicit return value.
        >
        > e.g.
        >
        > options.reduce(function (options, item) {
        > // first argument is modified
        > options[item] = true;
        >
        > // Missing return value
        > }, {}); // reduce is invoked with non primative second argument


        I think the error may be that you are using the wrong method: reduce when you should forEach. I am not sure how to test for that.
      • rodobrist
        ... No, I know when to use each method. Ill try to give another example, the JSLint source uses a method named array_to_object, which can be re-written like
        Message 3 of 5 , Nov 26, 2012
        • 0 Attachment
          --- In jslint_com@yahoogroups.com, "douglascrockford" <douglas@...> wrote:
          >
          > --- In jslint_com@yahoogroups.com, "rodobrist" <roderickobrist@> wrote:
          > >
          > > Hi guys,
          > >
          > > I was coding and I noticed that I often make the same error from using reduce.
          > >
          > > e.g.
          > >
          > > var options = ['nocaps', 'evil', 'node', 'browser'],
          > > optionsAsObject = options.reduce(function (options, item) {
          > > options[item] = true;
          > > }, {});
          > >
          > > // When it should be:
          > > var options = ['nocaps', 'evil', 'node', 'browser'],
          > > optionsAsObject = options.reduce(function (options, item) {
          > > options[item] = true;
          > > return options;
          > > }, {});
          > >
          > > Functions have a default/implied return value of undefined, this means if the array has more than one item this will throw an error.
          > >
          > > I'm suggesting that if the reduce method is invoked with a non primative second argument and function's first argument has an assignment to its property, there must be an explicit return value.
          > >
          > > e.g.
          > >
          > > options.reduce(function (options, item) {
          > > // first argument is modified
          > > options[item] = true;
          > >
          > > // Missing return value
          > > }, {}); // reduce is invoked with non primative second argument
          >
          >
          > I think the error may be that you are using the wrong method: reduce when you should forEach. I am not sure how to test for that.
          >

          No, I know when to use each method.

          Ill try to give another example, the JSLint source uses a method named
          array_to_object, which can be re-written like this:

          function array_to_object(array, value) {
          return array.reduce(function (object, key) {
          object[key] = value;
          return object; //I forget this line allot
          }, {});
          }

          I suspect that the reduce method is used like this commonly, and I often forget to type the "return object;" statement.

          This is why the function passed into array.reduce should have an
          explicit return value.
        • douglascrockford
          ... function array_to_object(array) { var result = {}; array.forEach(function (key) { result[key] = true; }); return result; }
          Message 4 of 5 , Nov 26, 2012
          • 0 Attachment
            --- In jslint_com@yahoogroups.com, "rodobrist" <roderickobrist@...> wrote:
            >
            >
            >
            > --- In jslint_com@yahoogroups.com, "douglascrockford" <douglas@> wrote:
            > >
            > > --- In jslint_com@yahoogroups.com, "rodobrist" <roderickobrist@> wrote:
            > > >
            > > > Hi guys,
            > > >
            > > > I was coding and I noticed that I often make the same error from using reduce.
            > > >
            > > > e.g.
            > > >
            > > > var options = ['nocaps', 'evil', 'node', 'browser'],
            > > > optionsAsObject = options.reduce(function (options, item) {
            > > > options[item] = true;
            > > > }, {});
            > > >
            > > > // When it should be:
            > > > var options = ['nocaps', 'evil', 'node', 'browser'],
            > > > optionsAsObject = options.reduce(function (options, item) {
            > > > options[item] = true;
            > > > return options;
            > > > }, {});
            > > >
            > > > Functions have a default/implied return value of undefined, this means if the array has more than one item this will throw an error.
            > > >
            > > > I'm suggesting that if the reduce method is invoked with a non primative second argument and function's first argument has an assignment to its property, there must be an explicit return value.
            > > >
            > > > e.g.
            > > >
            > > > options.reduce(function (options, item) {
            > > > // first argument is modified
            > > > options[item] = true;
            > > >
            > > > // Missing return value
            > > > }, {}); // reduce is invoked with non primative second argument
            > >
            > >
            > > I think the error may be that you are using the wrong method: reduce when you should forEach. I am not sure how to test for that.
            > >
            >
            > No, I know when to use each method.
            >
            > Ill try to give another example, the JSLint source uses a method named
            > array_to_object, which can be re-written like this:
            >
            > function array_to_object(array, value) {
            > return array.reduce(function (object, key) {
            > object[key] = value;
            > return object; //I forget this line allot
            > }, {});
            > }
            >
            > I suspect that the reduce method is used like this commonly, and I often forget to type the "return object;" statement.
            >
            > This is why the function passed into array.reduce should have an
            > explicit return value.


            function array_to_object(array) {
            var result = {};
            array.forEach(function (key) {
            result[key] = true;
            });
            return result;
            }
          • rodobrist
            ... Ok, thanks Doug. I suppose that construct is less prone to errors.
            Message 5 of 5 , Nov 26, 2012
            • 0 Attachment
              --- In jslint_com@yahoogroups.com, "douglascrockford" <douglas@...> wrote:
              >
              >
              >
              > --- In jslint_com@yahoogroups.com, "rodobrist" <roderickobrist@> wrote:
              > >
              > >
              > >
              > > --- In jslint_com@yahoogroups.com, "douglascrockford" <douglas@> wrote:
              > > >
              > > > --- In jslint_com@yahoogroups.com, "rodobrist" <roderickobrist@> wrote:
              > > > >
              > > > > Hi guys,
              > > > >
              > > > > I was coding and I noticed that I often make the same error from using reduce.
              > > > >
              > > > > e.g.
              > > > >
              > > > > var options = ['nocaps', 'evil', 'node', 'browser'],
              > > > > optionsAsObject = options.reduce(function (options, item) {
              > > > > options[item] = true;
              > > > > }, {});
              > > > >
              > > > > // When it should be:
              > > > > var options = ['nocaps', 'evil', 'node', 'browser'],
              > > > > optionsAsObject = options.reduce(function (options, item) {
              > > > > options[item] = true;
              > > > > return options;
              > > > > }, {});
              > > > >
              > > > > Functions have a default/implied return value of undefined, this means if the array has more than one item this will throw an error.
              > > > >
              > > > > I'm suggesting that if the reduce method is invoked with a non primative second argument and function's first argument has an assignment to its property, there must be an explicit return value.
              > > > >
              > > > > e.g.
              > > > >
              > > > > options.reduce(function (options, item) {
              > > > > // first argument is modified
              > > > > options[item] = true;
              > > > >
              > > > > // Missing return value
              > > > > }, {}); // reduce is invoked with non primative second argument
              > > >
              > > >
              > > > I think the error may be that you are using the wrong method: reduce when you should forEach. I am not sure how to test for that.
              > > >
              > >
              > > No, I know when to use each method.
              > >
              > > Ill try to give another example, the JSLint source uses a method named
              > > array_to_object, which can be re-written like this:
              > >
              > > function array_to_object(array, value) {
              > > return array.reduce(function (object, key) {
              > > object[key] = value;
              > > return object; //I forget this line allot
              > > }, {});
              > > }
              > >
              > > I suspect that the reduce method is used like this commonly, and I often forget to type the "return object;" statement.
              > >
              > > This is why the function passed into array.reduce should have an
              > > explicit return value.
              >
              >
              > function array_to_object(array) {
              > var result = {};
              > array.forEach(function (key) {
              > result[key] = true;
              > });
              > return result;
              > }
              >

              Ok, thanks Doug. I suppose that construct is less prone to errors.
            Your message has been successfully submitted and would be delivered to recipients shortly.