Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does jquery define the number of array elements

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how jquery defines the number of array elements". In daily operation, I believe many people have doubts about how jquery defines the number of array elements. Xiaobian consulted all kinds of information and sorted out simple and easy to use operation methods. I hope to help you answer the doubts about "how jquery defines the number of array elements"! Next, please follow the small series to learn together!

1. knowledge array

An array is a collection of data of a certain type, which can be integers, strings, or even objects.

Javascript does not support multidimensional arrays, but because arrays can contain objects (arrays are also an object), arrays can be nested with each other to achieve similar multidimensional array functions.

1.1 define an array

Declare an array of 10 elements

var a=new Array(10);

At this time, memory space has been opened for a, containing 10 elements. Call with array name plus [subscript], for example, a[2], but the element is not initialized at this time. The call will return undefined.

The following code defines a variable array and assigns values to it.

var a=new Array();

a[0]=10;

a[1]="aaa";

a[2]=12.6;

As mentioned above, objects can be placed in arrays, such as the following code

var a=new Array();

a[0]=true;

a[1]=document.getElementByIdx_x("text");

a[2]={x:11, y:22};

a[3]=new Array();

Arrays can be instantiated directly when assigned, for example

var a=new Array(1, 2, 3, 4, 5);

var b=[1, 2, 3, 4, 5];

a and b are arrays, but b uses an implicit declaration to create another instance. In this case, if alert(a==b) is used, false will pop up.

1.2 multidimensional array

In fact, Javascript does not support multidimensional arrays. In asp, you can use dim a(10,3) to define multidimensional arrays. In Javascript, if you use var a=new Array(10,3), an error will be reported.

But as mentioned before, arrays can contain objects, so you can declare an element in an array as an array again, for example,

var a=new Array();

a[0]=new Array();

a[0][0]=1;

alert(a[0][0]); //pop1

Assignment at the time of declaration

var a=new Array([1,2,3], [4,5,6], [7,8,9]);

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

The effect is the same, a is conventionally instantiated, b is implicitly declared, and the result is to generate a multidimensional array

1.3 Array literals

I really don't know how to call this in Chinese, text array?

When it comes to arrays, we have to talk about Array Literals. Arrays are actually special objects. Objects have unique properties and methods. By object name. Properties, objects. Method () to value and call, while arrays are indexed to value, Array Literals and arrays have a lot of similarities, are a collection of data types, but Array Literals fundamentally, is an object, declaration and call, and array is different

var aa=new Object();

aa.x="cat";

aa.y="sunny";

alert(aa.x); //pop cat

Create a simple object, usually called by aa.x, and if it is considered Array literals, alert(aa["x"]) will pop up cat

var a={x:"cat", y:"sunny"};

alert(a["y"]); //pop sunny

This is another way to create objects, and the result is the same.

2. Operations on array elements

As mentioned above, you can read and write elements through arrays [subscripts].

Subscripts range from 0-(23(superscript 2) -1), and arrays are automatically converted to object types when subscripts are negative, floating, or even Boolean, for example

var b=new Array();

b[2.2]="XXXXX";

alert(b[2.2]); //-> XXXXX

This corresponds to b["2.2"]="XXXXX"

2.1 Loop of arrays

var a=[1,2,3,4,5,6];

for(var i=0; i

alert(a[i]);

}

This is the most common, go through the array, the code will pop up 1 to 6 in turn

There's also a common

var a=[1,2,3,4,5,6];

for(var e in a){

alert(e);

}

For example, the following is an example of a structure statement used on an object. For example, the following structure statement is used on an object.

var a={x:1,y:2,z:3};

for(var e in a){

alert(e + ":" + a[e]);

}

In this case, e takes the attribute name, i.e. x, y, x, and to get the value, it takes the array name [attribute], so a[e] is equivalent to a["x"], a["y"], a["z"]

2.2 array common function

concat

appends an array to an existing array and returns a new array without affecting the existing array

var a=[123];

var b="sunnycat";

var c=[""ido"];

var d={x:3.14, y:"SK"};

var e=[1,2,3,4,[5,6,[7,8]]];

alert(a.concat(b)); // -> 123,sunnycat

alert(a); // -> 123

alert(b.concat(c, d)); // -> sunnycat Object]

alert(c.concat(b)); // ->

alert(e.concat(11,22,33).join(" # ")); // -> 1 # 2 # 3 # 4 # 5,6,7,8 # 11 # 22 # 33

It should be noted that it can only be used for arrays or strings. If the connected (the previous a) is a numeric value, a Boolean value, or an object, an error will be reported. When the string is connected to the array, the string will be spliced with the first element of the array to form a new element, and the array will be added to the string.(I don't know why, please disclose it to those who know). For arrays containing arrays and objects, the connection will remain unchanged.

At this point, the study of "how jquery defines the number of array elements" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report