JS in operator, use of foreach and map-basic knowledge Summary------peng kee (016)

Source: Internet
Author: User

In Operator:

<script>/*1. Ability to traverse the properties of an object*/    varobj = {        ' Name ': ' Jack ', Age:20    };  for(varKeyinchObj) {console.log (key+ ":" +obj[key]); }    /*2. You can also determine whether an object can access a property, regardless of whether the property is defined in the current object*/Console.log (' Name 'inchobj);//trueConsole.log (' toString ')inchobj);//trueConsole.log (' toString1 'inchobj);//false</script>

Use of Foreach:

<script>/*foreach is used to iterate over an array*/    //var arr= [123,2,4,35,435,65,7,658,67,98];    /*Value: The current traversal to the element value Index: current element indexed arr: the currently traversed array **/    /*Arr.foreach (function (value,index,arr) {console.log (value + ":" + index + ":" + arr); })*/    /*Customizing the foreach function*/Array.prototype.myForEach=function(callback) { for(varI =0;i<arr.length;i++) {callback ( this[i],i, this); }    }    vararr1= [123,2,4,35,435,65,7,658,67,98]; Arr1.myforeach (function(value,index) {console.log (value+ ":" + index + ":" +arr1); });</script>

Use of Map:

<script>varArr= [123,2,4,35,435,65,7,658,67,98]; /*double the array value and store it in an array*/    /*1. Use the for*/    //var newarr=[];    /*for (var i = 0; i < arr.length; i++) {var obj = arr[i];    Newarr.push (obj*2); } console.log (newArr);*/    /*2. Using the foreach*/    /*Arr.foreach (function (value,index,arr) {newarr.push (value*2);    }); Console.log (newArr);*/    /*3. Using the map function*/    /*arr.map (function (value,index,arr) {newarr.push (value*2);    }); Console.log (newArr);*/    /*Map stores the results in a newly generated array and returns*/   /*var newArr = arr.map (function (value,index,arr) {return value * 2;    }); Console.log (newArr);*/    /*how map is implemented*/Array.prototype.myMap=function(callback) {vartemp = [];  for(vari=0;i< this. length;i++){            varv = Callback ( this[i],i, this); if(v) {temp.push (v); }        }        returntemp;    }; varNEWARR = Arr.mymap (function(value,index,arr) {returnValue * 2;    }); Console.log (newArr);</script>

JS in operator, use of foreach and map-basic knowledge Summary------peng kee (016)

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.