Skip to content
This repository

Allowing string primitives to be used in _.each and similar functions... #561

Closed
wants to merge 1 commit into from

2 participants

pepkin88 Jeremy Ashkenas
pepkin88

... by converting obj to Object. Right now i in obj causes TypeError.

Jeremy Ashkenas
Owner

Nope -- Underscore collection functions are supposed to work on arrays (arraylikes) and objects ... not on strings. See previous tickets discussing this.

Jeremy Ashkenas jashkenas closed this April 19, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Apr 19, 2012
pepkin88 Converting obj to Object allows string primitives to be used in _.eac…
…h and similar functions.
5fb5333
This page is out of date. Refresh to see the latest.

Showing 1 changed file with 2 additions and 1 deletion. Show diff stats Hide diff stats

  1. 3  underscore.js
3  underscore.js
@@ -75,8 +75,9 @@
75 75
     if (nativeForEach && obj.forEach === nativeForEach) {
76 76
       obj.forEach(iterator, context);
77 77
     } else if (obj.length === +obj.length) {
  78
+      var objectObj = new Object(obj);
78 79
       for (var i = 0, l = obj.length; i < l; i++) {
79  
-        if (i in obj && iterator.call(context, obj[i], i, obj) === breaker) return;
  80
+        if (i in objectObj && iterator.call(context, obj[i], i, obj) === breaker) return;
80 81
       }
81 82
     } else {
82 83
       for (var key in obj) {
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.