Vivid Array object in JavaScript _javascript skills

Source: Internet
Author: User

Objective

The array object in JavaScript, which we often say, is primarily used to encapsulate and manage multiple data of any type.

Array objects are supported by all major browsers.

We all know that the array instance has these four methods: Push, pop, shift, Unshift. We all know the push + POP implementation stack, SHIFT + push implementation queue. Here not to discuss what advanced after out, FIFO.

But one side of this question is going to use these methods.

Topic

Spiral Matrix This noun, in the background language may be very familiar with, he is a two-dimensional array, what is his characteristics? Take a look at the picture below:

Above is a spiral matrix from the outside to the inside, his arrangement is to start from the periphery, a circle around the most inside, like a hovering snake.

Analysis and Solution

To get to the point, this September Tencent school recruit online written questions have a spiral matrix problem, incoming given number n, print out the N*n spiral matrix, when the rookie did not do it, time after their own computer thinking, and then suddenly understand the mystery.

Although the blogger didn't record the code at the time, I first defined a two-dimensional array of n*n, to get the need to go around a few layers, such as the above is 2 layers, and then the cycle several times, in the internal use of four for loop, is up or down to the definition of two-dimensional array interpolation content, the specific code can not on, anyway, the method is very stupid, and is not the focus of this article, the following into this chapter of the topic:

A few days ago I do a problem on the codewars, encountered a problem of a spiral matrix, it is required to write a function, given a matrix two-dimensional array parameters, return an array, the array of element order is the path of the spiral matrix.

For example:

function Getlinear (spiral) {
 //... Do some action 
}

var arr = [
 [1,2,3], [
 4,5,6],
 [7,8,9]
]

getlinear (arr)//back [1,2,3,6,9,8,7,4,5]

The above example clears to see that the Getlinear function is outputting the incoming ' Helix matrix ' in order with a one-dimensional array (I don't know how to say that, anyway, this two-dimensional array is circled like a snake to form a one-dimensional array).

The first time I saw the problem, I remembered the question of Tencent's enrollment, and then the blogger wrote it with a similar four for loop and then submitted it. This site has a function is that you finish the topic can see other people do the code, bloggers carefully point to open the answer list, wow, the first one is deeply attracted me. Although you do not remember the source code written by others, but roughly this:

function Getlinear (spiral) {

  var item;

  var linear = [] while

  (item = Spiral.shift ()) {
  //up
  linear = linear.concat (item)

  //Right for
  (var i = 0; i < spiral.length; i++) {
   Linear.push (Spiral[i].pop ())
  }

  //down
  linear = Linear.concat (Spiral.pop (). reverse ())

  //Left for
  (var i = spiral.length-1 i >= 0; I-) {
   Linear.push (Spiral[i].shift ())
  }

  }
  return linear
 }

For rookie level of me, just at the beginning still a bit Meng, because and my thinking is not the same, read a while to discover the mystery. Compared to what I wrote, this code does not need to consider whether the incoming is a n*n array, he can parse any array such as 2*3 array.

And the code is absolutely concise, for a certain basis is also very easy to understand.

If you're a little confused, look down, my graphic interpretation

On
 linear = Linear.concat (item)

The item is the first element of a two-dimensional array, which is the first array, and it moves the divisor group and returns, as follows:

After this line of code, the original array becomes the following:

Next, we need to add 5 6 7 to the array to be returned, that is, the last element of each array element in the two-dimensional array, and we can get it by pop:

Right for
  (var i = 0; i < spiral.length i++) {
   Linear.push (Spiral[i].pop ())
  }

Then the original two-dimensional array becomes the following:

Next we're going to get the last line 10 9 8 and invert the two-dimensional array pop out the last array and then reverse him.

Lower
 linear = Linear.concat (Spiral.pop (). reverse ())

At this point the original two-dimensional array is like this:

To get to the left is similar to the right, just turn the pop into shift:

Left for
(var i = spiral.length-1 i >= 0; I-) {
 Linear.push (Spiral[i].shift ())
}

The original two-dimensional array becomes:

At this point, a lap is over, and then while deciding whether to enter the next lap.

Summarize

Well, the content of this article is here, a seemingly not simple topic, in the flexible array has become so simple, I hope the content of this article to you JS Novice can help, if there is doubt you can message exchange.

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.