Summary of the javascript array method, javascript Array

Source: Internet
Author: User
Tags javascript array

Summary of the javascript array method, javascript Array

In the development process, we always encounter common problems, such as flattening a two-dimensional array into a one-dimensional array or a three-dimensional array into a one-dimensional array. When you encounter these problems, you will always rethink them. It is better to extract them and summarize them.

The following is an example of how to flatten a multi-dimensional array into a one-dimensional array. You can use a better method to publish it in the message area.

The first method is recursive processing. The Code is as follows:

Var arr = [1, 2, 3, [3, 3, 3, [5, 4, 5, 6, 6, 7, 8], [333,444 4]; function product () {// 1. Create an empty array, var newarr = []; // 2, and return a function, the function parameter is the return function flatten (arr) {// 3, the loop array. If it is not input, insert it into the newarr // if it is an array, recursively call faltten and merge the result with newarr for (var t of arr) {if (! Array. isArray (t) {newarr. push (t);} else {newarr. concat (flatten (t)} return newarr} var flatten = product (); console. log (flatten (arr ))

The execution result is:

The above method is quite satisfactory. For details about the code, see annotations. The following method applies some new features of the javascript language. The Code is as follows:

var arr = [1, 2, 3, [3, 3, 3, [5, 4, 5, 6, 6, 7, 8]], [333, 4444]];function flatten(arr){ return arr.reduce(function(pre,cur){  if(!Array.isArray(cur)){   return [...pre,cur];  }else{   return [...pre,...flatten(cur)]  } },[])}console.log(flatten(arr))

The above Code uses a new ES6 extension cloud algorithm "... "," [... abc ,... fff] "is equivalent to abc. concat (fff), which is more intuitive and clear, and uses the reduce method. Reduce is a method of arrays in javascript.

When the array calls the recduce method, two parameters can be passed. The first parameter is the callback function, and the second parameter is an initial value. Two parameters need to be passed in the callback function. The first parameter is the return value of each function execution, and the second parameter is the value of the array corresponding to the current index. The second parameter of reduce can be omitted. If it is omitted, the parameters called by the callback function for the first time are the values of the first and second items of the array. If it is not omitted, the first parameter of the callback function is the initial value. In the example above, the second parameter of reduce sets an empty array.

Reduce document address: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce

The first one is better to understand, and the second is to use and understand reduce functions.

Related Article

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.