Home > Web Front-end > JS Tutorial > body text

The for in method traverses object properties and optimizes nested loops

巴扎黑
Release: 2016-11-25 09:59:01
Original
1649 people have browsed it

①The for in statement loops through the JavaScript object. Each time it loops, it will obtain a property or method of the object.

Syntax:
for(valueName in ObjectName){
// Code block
}
Among them, valueName is the variable name, which saves the name of the attribute or method. The value of valueName will change every time it loops.

Example:
var item_info = {'ITEM000000':{
    barcode: 'ITEM000000',
    name: 'Coca-Cola',
      unit: 'bottle',
    price: 3.00
},'ITEM000004':{
        barcode: 'Item000004',
name: 'battery',
unit: ',
price: 2.00}};
For (var j in item_info) ----- & gt; Change. 】
{shopping_list='name:'+item_info[j].name}

②Create an empty object instead of nested loop for optimization

var item_info = {};
for(var i=0;i  if(item_info[inputs[i].barcode]){---->[Indicates that the condition exists]
          item_info[inputs[i].barcode].count+= 1;----> ;[It’s best to use targeted data to represent keys]
       } else{
                                                                        use       through using ’ through ’ through ’ through’ use ’ through ’ through ’ through ’ through ’s ’ through ’ through ‐ to ‐ ‐‐‐‐‐‐ and ‐ } to ③ Naming

is generally a noun and a function is a verb

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!