JS basic knowledge objects and arrays

Source: Internet
Author: User

An object is a collection of unordered attributes, each of which has its own name and value.

There are many ways to create objects, as I've already said in Chapter I.

How to traverse an object:

var x;
var txt= "";
var person={fname: "Bill", lname: "Gates", age:56};

for (X-in-person)//x is actually a variable that represents the property of the Person object
{
Txt=txt + person[x]; person[property] is equal to person. property
}

alert (txt); All values in the output object

If the value reads one of the properties, only the Var Value=person is required. Property name, you can.

You can also assign values to object properties, person. Attribute name =value;

Array: is a collection of ordered values.

The creation of arrays is also described in the first one.

Traversing an array is no different than traversing an object, and the method above is equally possible.

But you can iterate through the array with the array length

var aa=[2,3,4,5,6,7];

for (Var i=0;i<aa.length;i++) {

Alert (Aa[i]); Output each value

}

About arrays, it has a lot of methods. Here's a look at these common methods.

The join () method converts all the values of an array to a string and then joins them.

Eg:var a=[1,3,4];

var s=a.join (); s=, "the"

The reverse () method reverses the order of the array elements and returns the inverted array.

var a=[1,23,5];

A.reverse (); A=[5,23,];

Sort () method, sorting the array

If parameters are not given, they are sorted alphabetically by group

var a=["F", "s", "a", "T", "H"];

A.sort (); A=["A", "F", "H", "s", "T";

There is also a kind of parameter, so that the array from small to large rows.

var a=[33,4,55,666];

A.sort (function (A, b) {return-A-b}); Turn into 4,33,55,666

Concat () Creates and returns an array.

var a=[1,2,3];

A.concat (4,5); return [1,2,3,4,5];

A.concat ([4,5])//return [1,2,3,4,5];

A.concat (4,[5,[6,7]])//return [1,2,3,4,5,[6,7]] cannot expand an array of elements

Slice () returns a fragment of the specified array, a sub-array

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

A.slice (0,3)//return [+]

A.slice (3)//return [4,5]

A.slice (1,-1); Back [2,3,4]-1 for the penultimate (last)

Splice () Inserts or deletes an array element and modifies the array on the original array

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

A.splice (4); return [5,6,7,8], a=[1,2,3,4]//means delete data on the original array, return the deleted array element

A.splice (//return) [2,3], a=[1,4]

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

A.splice (2,0, "a", "B")//delete first from 2 position, 0 elements after, insert parameter from the position of the first parameter 3,4,5 ... return [], a=[1,2, "a", "B", 3,4];

The push () method adds a new element to the tail of the array, returning the length of the array

The Pop () method removes the last element and returns the deleted value.

var a=["QQ"];

(A.push); a=["QQ", []

A.pop (); a=["QQ", 1]

Unshift () inserts a new element in the head of the array, returning the new array length

Shift () deletes the first element of the array and returns the deleted value.

var a=[];

A.unshift (a); a=[1,2,3]

A.shift (); a=[2,3]

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.