3 properties and 14 methods of JavaScript array

attribute
constructor: returns a reference to the array function that created this object.
The constructor property returns a reference to the array function that created this object

var test=new Array();

if (test.constructor==Array)
{
document.write("This is an Array");
}
if (test.constructor==Boolean)
{
document.write("This is a Boolean");
}
if (test.constructor==Date)
{
document.write("This is a Date");
}
if (test.constructor==String)
{
document.write("This is a String");
}

</script>

The output result is:

This is an Array

length: sets or returns the number of elements in the array.
The length property sets or returns the number of elements in the array

var arr = new Array(3)
arr[0] = "John"
arr[1] = "Andy"
arr[2] = "Wendy"

document.write("Original length: " + arr.length)
document.write("<br />")

arr.length=5
document.write("New length: " + arr.length)

</script>

The output result is:

Original length: 3
New length: 5

prototype: enables you to add properties and methods to objects.
The prototype attribute is relative to the attributes and methods you want to add

function employee(name,job,born)
{
this.name=name;
this.job=job;
this.born=born;
}

var bill=new employee("Bill Gates","Engineer",1985);

employee.prototype.salary=null;
bill.salary=20000;

document.write(bill.salary);

</script>

The output result is:

20000

method
concat(): concatenate two or more arrays and return results.
The concat() method connects two or more arrays and returns the result

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

</script>

The output result is:

1,2,3,4,5

join(): put all elements of the array into a string. Front end training Elements are separated by the specified separator.
The join() method puts all the elements of the array into a string, separated by the specified separator

var arr = new Array(3)
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"

document.write(arr.join())

</script>

The output result is:

George,John,Thomas

pop(): deletes and returns the last element of the array
The pop() method deletes and returns the last element of the array

var arr = new Array(3)
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"

document.write(arr)

document.write("<br />")

document.write(arr.pop())

document.write("<br />")

document.write(arr)

</script>

The output result is:

George,John,Thomas
Thomas
George,John

push(): adds one or more elements to the end of the array and returns the new length.
The push() method adds one or more elements to the end of the array and returns a new length.

var arr = new Array(3)
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"

document.write(arr + "<br />")
document.write(arr.push("James") + "<br />")
document.write(arr)

</script>

The output result is:

George,John,Thomas
4
George,John,Thomas,James

reverse(): reverse the order of the elements in the array.
The reverse() method reverses the order of the elements in the array

var arr = new Array(3)
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"

document.write(arr + "<br />")
document.write(arr.reverse())

</script>

The output result is:

George,John,Thomas
Thomas,John,George

shift(): deletes and returns the first element of the array
The shift() method deletes and puts back the first element of the array

var arr = new Array(3)
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"

document.write(arr + "<br />")
document.write(arr.shift() + "<br />")
document.write(arr)

</script>

The output result is:

George,John,Thomas
George
John,Thomas

slice(): returns the selected element from an existing array
The slice() method returns the selected element from an existing array

var arr = new Array(3)
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"

document.write(arr + "<br />")
document.write(arr.slice(1) + "<br />")
document.write(arr)

</script>

Output results:

George,John,Thomas
John,Thomas
George,John,Thomas

sort(): sort the elements of the array
The sort() method is to sort the elements of the array

var arr = new Array(6)
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"
arr[3] = "James"
arr[4] = "Adrew"
arr[5] = "Martin"

document.write(arr + "<br />")
document.write(arr.sort())

</script>

The output result is:

George,John,Thomas,James,Adrew,Martin
Adrew,George,James,John,Martin,Thomas

splice(): delete the element and add a new element to the refrigerator array
The splice() method deletes the element and adds a new element to the array
Example: create a new array and add an element to it

var arr = new Array(6)
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"
arr[3] = "James"
arr[4] = "Adrew"
arr[5] = "Martin"

document.write(arr + "<br />")
arr.splice(2,0,"William")
document.write(arr + "<br />")

</script>

The output result is:

George,John,Thomas,James,Adrew,Martin
George,John,William,Thomas,James,Adrew,Martin

Example: delete the element at i[2] and add a new element to replace the deleted element

var arr = new Array(6)
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"
arr[3] = "James"
arr[4] = "Adrew"
arr[5] = "Martin"

document.write(arr + "<br />")
arr.splice(2,1,"William")
document.write(arr)

</script>

The output result is:

George,John,Thomas,James,Adrew,MartinGeorge,John,William,James,Adrew,Martin

toSource(): returns the source code of the object
The toSource() method returns the source code of the object

function employee(name,job,born)
{
this.name=name;
this.job=job;
this.born=born;
}

var bill=new employee("Bill Gates","Engineer",1985);

document.write(bill.toSource());

</script>

The output result is:

({name:"Bill Gates", job:"Engineer", born:1985})

toString(): converts an array into a string and returns the result
The toString() method converts an array into a string and returns the result

var arr = new Array(3)
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"

document.write(arr.toString())

</script>

The output result is:

George,John,Thomas

toLocaleString(): converts an array into a local array and returns the result
The toLocaleString() method converts the array to a local array and returns the result

var arr = new Array(3)
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"

document.write(arr.toLocaleString())

</script>

The output result is:

George, John, Thomas

unshift(): add one or more elements like the beginning of the array and return the length of the array
The unshift() method adds one or more elements like the beginning of the array and returns the length of the array

var arr = new Array()
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"

document.write(arr + "<br />")
document.write(arr.unshift("William") + "<br />")
document.write(arr)

</script>

Output results:

George,John,Thomas
William,George,John,Thomas

valueOf(): returns the original value of the array object.
The valueOf() method returns the original value of the array object.

Syntax:

arrayObject.valueOf()

Keywords: Javascript Front-end html5

Added by digitalecartoons on Thu, 09 Dec 2021 06:56:08 +0200