forEach usage

Javascript array loop traverses the forEach
array loop variable, the first is the method of for(var i =0; i< array.length; i++){}, in addition, the simpler forEach method can also be used.
Syntax: array.forEach(function (value){
              })
where array is an array object
         and value is a parameter, which is array[i] in the for loop.
E.g:
var input=[1,2,3,4,4];
var object = {};
for(var i=0;i<input.length;i++){
     if(!object[input[i]] ){
         object[input[i]]=1;
        }
      else{
             object[input[i]] +=1;
       }
}

Change the forEach loop to:
var input=[1,2,3,4,4];
var object = {};
input.forEach(function(value){
         if(!object[value]){
             object[value]=1;
          }
          else{
               object[value]+=1;
          }
})

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327040345&siteId=291194637