In JavaScript for: In Loop trap

Source: Internet
Author: User
Tags hasownproperty

Long time no blog, but tonight was a small problem trapped, I want to continue to slack down. The small question is: Remove the same attribute from each element in an array, and then add the attributes together. Each element of an array is of the same type and contains multiple attributes, except that the values of the attributes differ.

At the outset, take the for in method to iterate over the algebraic group:

Assume that the array being iterated is items = [{A: ' Ten ', B: ' One ', C: '},{a ': ' + ', ' B: ' + ', C: '}]var ' count;for (item in items) {      count + = item[' a ' ];      }

It turns out that count is not the 10+20 I want. But Nan. Try ITEM.A to take the value of the A property, the result is the same. Finally there is no way, only one log out of each item's a property value, hairstyle whether it is item[' a '] or ITEM.A, the output is undefined. Have to rely on online power. The original JavaScript for in and Java for in have difference.

JavaScript provides a special loop (that is, for. In loop), used to iterate over the properties of an object or each element of an array, the loop counter in the for...in loop is a string, not a number. It contains the name of the current property or the index of the current array element.
Case one:

When traversing an object, the variable i, which is the loop counter, is the property name of the object:

In any case, it is not the object itself that is traversed, not the index is the property.

However, for-in is more than that simple, see case 2:

See, even the properties of the prototype have been output. If you are using a regular for loop, how to:

In this case, the properties of the prototype are not being exported.

Original for.. The in loop iterates through the methods and properties in a type's prototype (prototype), so this can lead to unexpected errors in the code. To avoid this problem, we can use the hasOwnProperty () method of the object to avoid this problem, and the hasOwnProperty () method returns True if the property or method of the object is non-inherited. That is, the check here does not involve properties and methods inherited from other objects, only properties that are created directly in the particular object itself.



Operation Result:
Admin
Manager
Db

With the hasOwnProperty method, you can avoid traversing to inherited methods and properties. That being the case, if it's just a simple traversal of an array element, the usual for+i comes directly into effect.

Finally, by the way, make a note of the JavaScript conversion method of character-to-shaping: parseint

In JavaScript for: In Loop trap

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.