Functions and array traps in ES6

Source: Internet
Author: User

function Deconstruction of objects
When we detach from the front and back, the backend often returns JSON-formatted data, and the good intentions of the front end is to pass the JSON format data directly to the inside of the function as a parameter. ES6 provides us with such an assignment of deconstruction.
Let JSON = {    A:' xzblogs ',    B:' Little Smart '}function Fun ({a,b= ' if no B with this substitute '}) {    Console.log (A, b);} Fun (JSON);
is not the feeling of convenience a lot, we no longer have to pass the parameters.
function Deconstruction of arrays
The function can deconstruct the JSON, and that's a much better solution to our array, let's look at the code below. We declare an array, then write a method, and finally use ... To perform the deconstruction assignment.
Let arr = [' xzblogs ', ' Little Wise ', ' Zachary ']; function Fun (a,b,c) {    console.log (a,b,c);} Fun (... arr.);
In usage
In is used to determine whether a value exists in an object or array. Let's take a look at how to tell if there is a value in the object.
Object judgment
Let obj={    A:' xzblogs ',    B:' Little Wise '}console.log ( in obj);  // true
Array judgment
Let's take a look at the drawbacks of ES5 judgment, which used to be judged by the length property, which means that there is no array element for 0. But that's not accurate, or there are drawbacks in real-world development.
Let arr=//5 above the code output 5, but the array is actually all empty values, this is a pit ah. That will solve the problem with ES6 in.
Let arr=[,,,,,];console.log (in//false let arr1=[' xz ', ' Little Wise '); Console.log ( in arr1);  // true Note: The 0 here refers to whether the array subscript position is empty.
Array Traversal method 1.forEach
Let arr=[' xzblogs ', ' Little Wisdom ', ' Zachary '];arr.foreach ((val,index)=Console.log (index,val)); The feature of the Foreach loop is that it is automatically omitted as an empty array element, which is equivalent to simply sifting us out of space. Sometimes it can be a disservice to us. 
2.filter
Let arr=[' xzblogs ', ' Little Wisdom ', ' Zachary '];arr.filter (x=console.log (x)); This approach I've told in Vue, he actually has a looping function, Here we are reviewing again. 
3.some
Let arr=[' xzblogs ', ' Little Wise ', ' Zachary '];arr.some (x=>console.log (x));
4.map
Let arr=[' xzblogs ', ' Little Wise ', ' Zachary '];console.log (arr.map (x= ' web ')); Map is a replacement here, and this is a follow-up course that will be explained in detail. 
Array conversion string
In development we often encounter the output of the array into a string form, we learn two methods today, you should pay attention to the difference between the two methods.
Join () method
Let arr=[' xzblogs ', ' Little Wisdom ', ' Zachary '];console.log (arr.join (' | ') ) ; the join () method is to add some spacing between the elements of the array, which is useful in development. 
toString () method
Let arr=[' xzblogs ', ' Little Wise ', ' Zachary '];console.log (arr.tostring ()); The conversion is only separated by commas. 

Functions and array traps in ES6

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.