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

Source: Internet
Author: User
Tags javascript array

Today, there is a problem: the use of Easyui, the height of the DataGrid table changes (set to a fixed height), read a half-day document, also looked at some of the web, but also is not out, a little depressed, this easyui in some cases is really useful, It's a bit of a hassle to be able to adjust at random.

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

Yesterday, in the implementation of the drag-and-drop trajectory, the application of the array was a critical step, so we deliberately looked at some of the JavaScript array operations, here to record:

"Adding and deleting" These four words believe is not unfamiliar, most of our work is in these operations, and these operations there is a premise: there is a let you delete and change the object, if not exist, then we first to create a:

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 we can clearly see that the first one is empty, and the second has two comma "," Then there are 3 empty, followed by the normal output , but

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


We have four methods in the process of creation, which is better, we can not judge, different circumstances, but there is a situation we need to pay special attention to

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

In fact, this is quite a bit confusing, but a square bracket, the meaning is not the same,

The previous one is to create an array of length 3, the latter is to create an array of length 1, and the first value is 3 , so put together so that we can all understand, but give you any one, if not careful, it is very likely to go wrong.

On the other hand, the two arrays of the new Array ([5,2]) and [5,2] are the same, and are answered in the view.

2. View

Now that the creation is successful, we can look at the properties or values of this object, or something else. 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]);}
Here is not a show, just a few confusing places to record:

*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 solve the problem created 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", In fact, using the 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 more clear, but one thing I still have some doubts:

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

And if Var t=e "2", then output typeof (T), is also number, which is why?

3. Delete

If the above principle is more, then here's a few more ways.

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

shift () with the deletion of the last one, it is more concerned about the first, symbolizing, this method is used to delete the first element, and return the operation of the element value ;

. Splice (here,num,newitem,newitem2,... This method has parameters, the overall meaning is to delete the number to the first, and to return the deleted value as an array, and the first bit of the parameter is in the array of the beginning of the number, start deletion, here is starting from 0, the second argument is several, if 0, Then do not delete, if it is 1, then from the beginning of the number of the beginning of the first only 1 values, in turn, and if the following arguments, then it should be added to the category, where the first explanation, that is, after the operation of the previous, and then in just the operation bit insert the following parameters, that is, the new value.

4. Increase

The front unexpectedly introduced to splice, here should say increase, splice to a certain extent is increased, if the 2nd parameter is 0, and there is also a 3rd or more parameters, it is increased, and if the 2nd parameter is not 0, and there are more parameters , it is modified , so the changes are no longer said alone.

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

The relative . Unshift () is added at the front end of the array, which corresponds to the deletion.

5. Sorting

Have to say, this was a bitter lesson, in that bubble sort is not familiar, once used this method to sort, but the interview, Leng two did not think up, reverse order AH or yourself you suppress a half-day write ugly, how painful understanding AH was

. Sort () what a painful understanding.

. Reverse () reverse order, the front of the most back, the last face to the front, how painful the understanding AH

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 them are returned new oh, after stitching, new one, oh, new OH

Here for the time being recorded so much, and did not find too many useful operations, here is a little knowledge:

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


The way of heaven, the loss of more than the lack of compensation ..... You think of who, warrior Guo Jing Ah, as long as there is an upward heart, always see the sun rise that moment ....



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.