SoFunction
Updated on 2025-04-10

Sharing of practical operation techniques for Array in JavaScript

1. About Array

Array creation is very flexible. You can use the Array constructor or create an array "literal" directly.

var arr = new Array(); //[]
var brr = Array();  //[] Both are equivalent
var arr = Array(3); //[]
;   //3 Empty array of length 3
var arr = Array(22,33,'qq',{}); //[22, 33, "qq", Object]
var brr = [22,33,'qq',{}];  //Same as above

Array is a built-in object for JavaScript. Yes, although it is an array, it is also an object! !

Use typeof to judge will return an Object! andThe method can more accurately determine its type.

var a = [];
typeof a;   //object
(a); //true

2. Common methods

push() method

pushThe method can add one or more elements to the end of the array and return the length of the changed array!

Notice:① It returns the length of the array, not the array!

②This method will change the original array! !

var arr = Array(22,33,'qq',{});
('weibo');    //5
arr       //[22, 33, "qq", {}, "weibo"]

This is what we need to use when we want to merge two arrays

var a = [22,33];
var b = [44,55];

(a, b)
// or(a, b)
// or(44,55);   //Array at this timea = [22,33,44,55];

Be careful not to write the following! !

(b);
a;      //[22,33,[44,55]]
;     // 3 !!
(a);   //[22, 33, Array[2]]

Written directly(b), will add b as an element to a, and it will not get the ideal effect!

If two object arrays now need to be merged like this:

var a = [
 {name: 'Stark', value: 'Ironman'},
 {name: 'Cap' , value: 'Oldman'}
];
var b = [
 {name: 'Jerry', email: 'Jerry@'},
 {name: 'Lory' , email: 'Lory@'},
 {name: 'susan', email: 'susan@'}
];
//Error writing(b);   //3
(a);  //[Object, Object, Array[3]]
//Correct writing(); //5
(a);  //[Object, Object, Object, Object, Object]

pop() method

andpushInstead, delete the last element of the array and return the deleted element:

var a = ['qq', 'weibo', 'weixin'];
();        // 'weixin'
a;         // ['qq', 'weibo']

join() method

Separate the array according to the corresponding parameters and return it as a string. If the parameter is empty, use the ',' separation. This method does not change the original array:

var a = [1, 2, 3, 4];
(' ')  // '1 2 3 4'
(' | ')  // "1 | 2 | 3 | 4"
var b = () // "1,2,3,4"
(a); // [1, 2, 3, 4]
(b); // "1,2,3,4"

concat() method

It can combine multiple numbers and return a new array, but the original array remains unchanged:

var a = [22,33];
var b = [44,55];
var c = (b);
(a);  //[22, 33]
(b);  //[44, 55]
(c);  //[22, 33, 44, 55]
var a = [{name: 'tom', email: 'tom@'},
   {name: 'peter', email: 'peter@'}];
var b = [{name: 'Jerry', email: 'Jerry@'},
   {name: 'Lory', email: 'Lory@'},
   {name: 'susan', value: 'susan@'}];
var c = (b);
c;   // [{name: 'tom', email: 'tom@'},
   // {name: 'peter', email: 'peter@'},
   // {name: 'Jerry', email: 'Jerry@'},
   // {name: 'Lory', email: 'Lory@'},
   // {name: 'susan', value: 'susan@'}]

map() method

mapThe method will call a function to each member of the array in turn, returning a new array processed by the function, but the original array will not be changed!

var numbers = [1, 2, 3];
var num  = (function (n) {  // [2, 4, 6]
     return n * 2;
    });
numbers;          //[1,2,3]

mapWhen the parameter of the function called by the method is one, this parameter represents the current member of the array; when the parameters are three, they are in sequence

Current memberelem,indexindex, the original array itselfarr

var brr = [1, 2, 3].map( function(elem, index, arr) {
 return elem * index;
});
brr; // [0, 2, 6]

mapThe method can also accept a second parameter, indicating when the callback function is executedthisThe object pointed to.

var arr = ['a', 'b', 'c'];

var brr = [0, 2].map(function(e){
 return this[e];
}, arr)
brr; // ['a', 'c']

In applications, sometimes using ajax technology requires dynamically converting parameter arrays into a url request.mapThe method will be very convenient, for example:

var b = [
 {name: 'Jerry', email: 'Jerry@'},
 {name: 'Lory', email: 'Lory@'},
 {name: 'susan', value: 'susan@'}];
 
var url = b.
   map(function(n){
    return  + "=" + 
   })
   .join("&");
   
(url); 
   //Jerry=Jerry@&Lory=Lory@&susan=susan@

Then add the ip address, action and method before the url to complete the dynamic url splicing required by ajax, for example:

var endURL = "localhost:8080/XXXX/" + eventAction + "!" + operation + "?"
    + url;

Summarize

The above is the entire content of this article. I hope it will be of some help to everyone's study or work. If you have any questions, you can leave a message to communicate.