Campbell
Campbell

Reputation: 725

string.split() returns extra items (functions)

This code:

allUID = $("#allservices_"+categoryUID).attr("value").split(',');

applied to an element with

value="
1298698f-62a0-41da-9303-563e317f97a1,
75b79dc8-873f-4e80-9174-64e3bf0b7e7b,
7add7028-dd32-40cc-baa3-a8dbdfe36dc0,
0b136659-19e5-4b58-9b58-23a5ba7383fe,
73a6a712-4aae-4101-ad36-77feea188aad,
8f5d7f01-b854-4a6f-9cf6-cc6554835c8a,
f0c1d8dc-a96d-402a-b41b-74f753a4c313,
770d1178-8c17-4e9d-8a31-bff8a15097b3
"

returns this:

0   "1298698f-62a0-41da-9303-563e317f97a1"
1   "75b79dc8-873f-4e80-9174-64e3bf0b7e7b"
2   "7add7028-dd32-40cc-baa3-a8dbdfe36dc0"
3   "0b136659-19e5-4b58-9b58-23a5ba7383fe"
4   "73a6a712-4aae-4101-ad36-77feea188aad"
5   "8f5d7f01-b854-4a6f-9cf6-cc6554835c8a"
6   "f0c1d8dc-a96d-402a-b41b-74f753a4c313"
7   "770d1178-8c17-4e9d-8a31-bff8a15097b3"
contains    function()
removeDoubles   function()
reversed    function()

Where do these extra functions come from?

Upvotes: 1

Views: 264

Answers (5)

jfriend00
jfriend00

Reputation: 707288

Iterate your array with this instead:

for (var i = 0, len = allUID.length; i < len; i++) {
   console.log(allUID[i]);
}

You will not see any of those extra properties when iterating over the array this way.

Those extra items come because you were iterating over the array with for (i in allUID) {} and that will pick up extra properties that have been added to the Array object by some framework or shim you are using. You should never iterate over an array with for (x in array). Use that form only for iterating all the properties of an object.

Upvotes: 0

arnorhs
arnorhs

Reputation: 10429

Something else in your codebase is adding those functions to the Array instance. I don't know what, since both jQuery and Prototype don't add those functions.

Note some of the answers indicate that the array's prototype has been changed - that is probably not the case. Depending on how you are outputting them, some library has probably added those functions to the array itself, instead of its prototype (which is bad).

Upvotes: 0

alex
alex

Reputation: 490203

It didn't happen for me.

Most likely, you are iterating over the resulting array with a for ( in ). Don't do that; use a normal for loop or jQuery's each().

Somewhere else in your code probably augments the Array prototype and doesn't specify the properties to be non enumerable (only possible in latest JavaScript versions with defineProperty() and friends).

jsFiddle (don't do this).

You should be using val(), rather than attr('value') too.

Upvotes: 5

Jonas H&#248;gh
Jonas H&#248;gh

Reputation: 10874

They're functions added to the array prototype (probably by a script you've included on the page). They're not part of the array as such, but they can be a problem when using for .. in to iterate over the array.

Extending the prototypes of built-in objects is not considered a good practice by most, so I would suggest replacing them if you can. Otherwise, use regular for loops over for .. in.

Upvotes: 1

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324630

Probably from jQuery. I wouldn't be surprised if jQuery adds Array.prototype.contains, Array.prototype.removeDoubles and Array.prototype.reversed.

Upvotes: -1

Related Questions