[Dada Front End] JavaScript Array Object

Keywords: Front-end Javascript Attribute PHP network

Author| Jeskson

Source|Dada Front End Bistro

JavaScript array object

An array object is a variable type used to store multiple values in a single variable.

Syntax for creating array objects:

new array();

new array(size);

new array(element0, element1, ..., elementn);

The parameter size represents the number of array elements, returns the array type, the length field is the value of the size, the parameters element0, element1,..., elementn, represents the parameter list, and the newly created elements of the array are initialized to these element values.

array object properties:

constructor property

Description: Returns a reference to the array function that created this object

length attribute

Description: Sets or returns the number of elements in an array

prototype attribute

Description: Allows you to add properties and methods to objects

array object method:

concat() method

Link two or more arrays and return results

join() method

Place all elements in an array into a string

pop() method

Delete and return the last element of the array

push() method

Add one or more elements to the end of the array and return the new length

reverse() method

Reverse the order of elements in an array

shift() method

Delete and return the first element of the array

valueOf() method

Returns the original value of an array object

unshift() method

Add one or more elements to the beginning of the array and return the length of the new array

toLocaleString() method

Converts an array to a local array and returns the result

toString() method

Converts an array to a string and returns the result

toSource() method

Returns the source code of the object

splice() method

Delete elements and add new elements to the array

sort() method

Sort elements of an array

slice() method

Return selected elements from an existing array

JavaScript constructor properties

Returns a reference to the array function that created this object

Format:

object.constructor
<script type="text/javascript">
 var dada = new Array();
 if(dada.constructor == Array){
  document.write("array");
 }
 
 if(dada.constructor == Boolean){
  document.write("boolean");
 }
 
 if(dada.constructor == Date){
  document.write("date");
 }
</script>

Result:

array

Using the constructor property:

function dada(name, age){
this.name = name;
this.age = age;
}

var dashu = new dada("dashucoding", 12);

document.write(dashu.constructor);

Result:

function dada(name,age){
this.name = name;
this.age = age;
}

JavaScript prototype properties

Allows you to add properties and methods to objects

Format:

object.prototype.name = value

Add properties to the object using the prototype property:

function dada (name, age){
this.name = name;
this.age = age;
}

var dashu = new dada("dashucoding", 12);

dada.prototype.job = null;

dashu.job = it;

document.write(dashu.job);

Result:

it

concat() method in JavaScript

Format:

arrayObject.concat(arrs1,arrs2,...,arrsn)
var a = [1,2,3];
document.write(a.concat(4,5));

Result:

1,2,3,4,5
var arr1 = new Array(2)
arr1[0] = "a"
arr1[1] = "b"

var arr2 = new Array(2)
var2[0] = 'c'
var2[1] = 'd'

document.write( arr1.concat(arr2))

Result:

a,b,c,d
var arr1 = new Array(2)
arr1[0] = "a"
arr1[1] = "b"

var arr2 = new Array(2)
var2[0] = 'c'
var2[1] = 'd'

var arr3 = new Array(2)
var3[0] = 'e'
var3[1] = 'f'

document.write( arr1.concat(arr2,arr3))

Result:

a,b,c,d,e,f

join() method in JavaScript

The join() method is used to place all elements in an array into a string.

var arr = [a,b,c];
console.log(arr.join());

//Result:
a,b,c
var arr = [a,b,c];
console.log(arr.join("-");

//Result:
a-b-c

JavaScript pop() method

Used to delete and return the last element of the array

Format:

arrayObject.pop()
var arr = [1,2,3];
console.log(arr.pop());

// 3

JavaScript push() method

Add one or more elements to the end of the array and return the new length

var arr = [1,2,3,4]
console.log(arr.push("5"));
// 5 Length

reverse() reverses its elements:

var arr = [1,2,3];
arr.reverse();

// 3,2,1

The shift() method deletes the first element of the array and returns the value of the first element

var arr = [2,3,4];
arrr.shift();

// 2

The valueOf method returns the original value of the array object

arrayObject.valueOf()

The unshift() method adds one or more elements to the beginning of the array and returns the new length.

The sort() method is used to sort the elements of an array.

function sortNumber(a,b){
 return a-b;
}

var arr = [1,2,4,9,7,3];
console.log(arr.sort(sortNumber));
arrayObject.toLocaleString()

First call the toLocaleString() method for each array element
 The resulting strings are concatenated using a region-specific delimiter to form a string.

toString() converts an array to a string

toSource() method:

Object Source Code

object.toSource()
function dada(name, age){
this.name = name;
this.age = age;
}
var dashu = new dada("dashucoding", 1);
console.log(dashu.toSource());

// ({name:"dashucoding", age: 1})

slice() method: the selected element can be returned from an existing array

var arr = [1,2,3,4]
console.log(arr.slice(1));
// 2,3,4

var arr2 = [a,b,c,d]
console.log(arr2.slice(2,3))

// c

splice() adds to the array, deletes the item, and returns the deleted item

index is required.Integer specifies where items are added/deleted, and negative numbers specify where items are located at the end of the array.

howmany is required.Number of items to delete.If set to 0, the item is not deleted.

Item1,..., itemX is optional.A new item added to the array.

var arr = [1,2,3,4,5];
arr.splice (2,0, 'a');

// [1,2,a,3,4,5]

var arr = [1,2,3,4,5];
arr.splice (2,1, 'a');

// [1,2,a,4,5]

Don't forget to leave a footprint of your study [compliment + collection + comment]

Author Info:

[Author]: Jeskson Original Public Number: Dada Front End Bistro. Welfare: Public Number replies to the big gift package for self-study materials (share in groups, just say anything you want, see if I have one)! [Reprint instructions]: Please explain the source of reprinting, thank you for your cooperation!~

Big Front End Development, Locate Front End Development Technology Stack Blog, PHP Background Knowledge Point, web Full Stack Technology Field, Data Structure and Algorithms, Network Principles and other understandable presentations to small partners.Thank you for your support and love!!!

If there are inadequacies in this number (e.g. copyright or other issues), please contact us in time to make corrections, which will be dealt with at the first time.

Please compliment!Because your approval/encouragement is my greatest motivation to write!

Welcome to your attention Dada CSDN!

This is a quality, attitudinal blog

Posted by gmp on Sat, 21 Dec 2019 12:11:09 -0800