1

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?

2
  • may i ask, how are you actually printing out each element of the array? Commented Oct 23, 2011 at 10:37
  • firebug in this case, in the code I'm returning values split from a couple of arrays, concatenated in a string html="uid: " + uid... etc Commented Oct 23, 2011 at 11:13

5 Answers 5

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

using for(i in ... ) and thanks for the tips. Probably Plone and friends. Thanks!
1

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.

Comments

0

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).

1 Comment

How do you know it's not the prototype? I think firebug will display an array like the above if the prototype has been augmented
0

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.

Comments

-1

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

1 Comment

jQuery doesn't touch the Array prototype.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.