[dada front end] javascript array object

Time:2021-9-13

[dada front end] javascript array object
Author | jeskson

Source: dada front end Tavern

JavaScript array object

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, the returned is the array type, the length field is the value of size, and the parameters element0, element1,…, elementn represent the parameter list. The elements of the newly created array will be 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 property

Description: lets you add properties and methods to an object

Array object method:

Concat() method

Link two or more arrays and return results

Join() method

Put all the elements in the array into a string

Pop() method

Deletes and returns the last element of the array

Push() method

Adds one or more elements to the end of the array and returns the new length

Reverse() method

Inverts the order of elements in an array

Shift() method

Deletes and returns the first element of the array

Valueof() method

Returns the original value of an array object

Unshift() method

Adds one or more elements to the beginning of the array and returns the length of the new array

Tolocalestring() method

Convert the array to a local array and return 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 the element and add a new element to the array

Sort() method

Sort the elements of the array

Slice() method

Returns the selected element 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

Use 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 attribute

Allows you to add properties and methods to objects

Format:

object.prototype.name = value

Use the prototype property to add properties to an object:

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 put all the elements in the 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

Adds one or more elements to the end of the array and returns 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 removes the first element of the array from it 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 a new length.

The sort () method is used to sort the elements of the 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 of each array element
Use the region specific separator to connect the generated strings 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: you can return the selected element from the 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 and deletes items to the array, and then returns the deleted items

Index is required. Integer that specifies the position of the item to be added / deleted. Use a negative number to specify the position from the end of the array.

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

Item1,…, itemx optional. Adds a new item 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 your learning footprints [like + collect + comment]

Author Info:

[author]: jeskson
Official account: dada’s front bar.
[welfare]: the official account answers the “information”, sends the gift package of self study materials (enter group to share, want to say anything ha, see if I have).
[reprint description]: please explain the source of reprint. Thank you for your cooperation~

Large front-end development, positioning front-end development technology stack blog, PHP background knowledge points, web full stack technology field, data structure and algorithm, network principle and so on. Thank you for your support, thanks for your love!!!


If the content of this number is not in place (for example, involving copyright or other problems), please contact us in time for rectification and will deal with it at the first time.


Please praise! Because your approval / encouragement is the biggest driving force for my writing!

Welcome to dada’s CSDN!

This is a blog with quality and attitude

[dada front end] javascript array object

Recommended Today

Blog Garden Background page dynamic effects

1. To set animation, you must first apply for permission 1.1 first enter [my blog park] and enter [settings] in [management] 1.2 find [blog sidebar announcement] and click [apply for JS permission] 1.3 write the content of application JS permission (examples are as follows) Dear blog administrator: Can you open JS permission for me? I […]