Home  >  Article  >  Web Front-end  >  The for in method traverses object properties and optimizes nested loops

The for in method traverses object properties and optimizes nested loops

巴扎黑
巴扎黑Original
2016-11-25 09:59:011641browse

①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

Statement:
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