Operation of an array in JS-------Day49

Source: Internet
Author: User

Today, there is a problem: how the height of the DataGrid table changes (set to a fixed height) in the use of Easyui, looked at the document for half a day, and also looked up from the web. Still stare is not out, a little depressed. This easyui in some cases really good, but it is a bit of trouble to adjust arbitrarily.

Can not be done first put on, go home to change a mood.

Yesterday in the implementation of the replay drag trajectory. The application of the array was a critical step, and the operation of some JavaScript arrays was deliberately viewed. Here to record the following:

"Additions and deletions to check" These four words believe is not unfamiliar. Most of our work is doing these things, and there's a major premise: there. There is an object that allows you to delete and change, assuming that it does not exist, let's start by creating one:

1. Create

var a=new array (), var b=new array (3), var c=new array ([' Qwe ', "12313", 123]), var d=["ASD", "Qwe", "ZXC";

Usually we create an array of the above four ways. And then

*alert (A + "----------" +b+ "---" +c+ "--------" +d);


So that we can see clearly. the first one is empty. And the second has two comma "," That means there are 3 empty, the back is the normal output , but

*alert (typeof (a) + "----------" +typeof (b) + "---" +typeof (c) + "--------" +typeof (d)), we all succeeded in creating an object.


There are four ways in which we create the process. Which one is better. We can't judge it, it's different from the situation. But there is a situation where we need to pay special attention

var test01=new array (3); var test02=new array ([3]);

In fact, it's quite confusing. Just a square bracket, the meaning is not the same.

The previous one is to create an array of length 3, and the latter one to create an array with a length of 1 and a first value of 3 , so that we can all be clear. But give you no matter what one, the hypothesis is not meticulous. There is a great possibility of error.

There is also one aspect of the new Array ([5,2]) and [5,2] which are the same, and are answered in the view.

2. View

Now that the creation is successful, we are able to view the properties or values of this object. or something.

First, we know that the best way to look at an array is to traverse it, usually by:

for (Var i=0;i<d.length,i++) {    alert (d[i]);}
There is not a single demonstration here, just a few confusing places:

*alert (a[0]+ "-----" +b[0]+ "---" +c[0]+ "-----" +d[0]+a[2]+ "-----" +b[2]+ "---" +c[2]+ "-----" +d[2 "); so we'll look at the output.


Here we have overcome the problems created in the above, the new Array ("5,2") and "5." 2 "or not, the former means that an array is created, the array contains an element, while the latter indicates that two elements were created, namely" 5 "and" 2 ". Its useful front to indicate the following words, should be written as: Var e=new Array ([' 5 '],[2],);

So I wrote down an output like this.

var e=new Array ([' Qwe '],["12312"],[123]), var f=[["1", "2"],["2", "3"]
*alert (a[0]+ "-----" +b[0]+ "---" +c[0]+ "-----" +d[0]+a[2]+ "-----" +b[2]+ "---" +c[2]+ "-----" +d[2]+ "---" +e[0]+ "--" +f[0 ] in combination with the above, what do you think the result will be?

So the principle is clearer. But there is one thing I still have some doubts about:

var d=["ASD", "Qwe", 123];var e=new Array (' qwe ', "12312", 123);
* * * * Suppose we output alert (e[2]+ "---" +typeof[e[2]]+ "--" +d[2]+ "---" +typeof (d[2])); The result is 123, but one is object. What is number one? Still a little bit confused .

Instead, assume that var t=e "2" and then output typeof (T). It's also number, and that's why?

3. Delete

Assume that the above principle is more points. Well, here's a few more ways.

. Pop () This was applied yesterday, removing the last element value of the array. and returns the element value. public Object Pop () {Here is the implementation of the last one removed; return object}

shift () with the last one removed, it's more about the first one. Symbolizing, this method is used to delete the first element and return the operation of the value of the element ;

. Splice (here,num,newitem,newitem2,... This method has a number of parameters. The whole meaning is to delete the first few to the first few to be. The deleted value is returned as an array, and the first digit of the number of parameters in the array begins to be deleted, starting from 0, and the second number being a few , assuming 0, without deleting. assumed to be 1. Then starting from the beginning of the first only to look at 1 values, in turn to push, and assuming that the following with the number of words, it should be in the category of accession, here first explained. Just after you've done the work. Then insert the following number in the previous operation bit, that is, the new value.

4. Add

The front actually introduced to the splice, here should say added, splice to a certain extent is added, assuming the 2nd parameter is 0. At the same time there is a 3rd or a lot of other parameters, it is added, in addition to assume that the 2nd is not 0, the same time there are many other parameters, it is changed , so the change will not be said alone.

And the previously used . push () is added at the end of the array

The relative . Unshift () is joined at the very front of the array. and the deletion.

5. Sorting

It has to be said that this was a dire pain lesson. At a time when the bubble sort was not familiar. Previously used this method to sort, but the interview, Leng two did not think up, reverse order AH or yourself you suppress a half-day writing ugly. What a pain to understand.

. Sort () what a painful understanding.

. Reverse () reverse order, the front of the most in the back, the last face to the front. What a painful understanding.

6. Interception, merging and copying of arrays

. Slice (Start,[end]), This application reminds me of substring (start,[end]), here is a new one, a new one, is new one OH

. Concat () stitching, all of which are returned by the new Oh. The new one after stitching, OH. Oh, new.

There's so much to record here temporarily. Nor did I find much practical action. Here is a little knowledge:

The return value of typeof () is six: number/string/boolean/object/function/undefined (remember the difference between it and null)


The way of the heavens. More than enough to fill the gaps ..... You think of who, hero Guo Jing Ah. Just have an upward heart, always see the moment the sun rises ....



Operation of an array in JS-------Day49

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.