Arrays in js

1, Array traversal

1.1 for cycle

The general method of array loop is as follows:

var a = [1, 2, 3, 4];
var length = a.length;
for(var i = 0; i < length; i++){
	console.log(a[i]);
}  

1.2 forEach cycle

var a = [1, 2, 3, 4];
a.forEach((v, k) => {
	console.log("v: " + v + " k: " + k);
});

1.3 for of traversal

var a = [1, 2, 3, 4];
for(var v of a){
	console.log(v);
}

1.4 for in

var a = [1, 2, 3, 4];
for(var k in a){
	console.log(a[k]);
}

2, Array delete element

In js, splice(index, length, [target]) method is used to delete, modify and add array elements. Index is the subscript of array elements, length is the length, and target is the element to be replaced.

2.1 deleting elements

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

2.2 replacement elements

var a = [1, 2, 3, 4];
a.splice(1, 1, "test");
console.log(a);// (4) [1, "test", 3, 4]
a.splice(2, 2, "test2");
console.log(a);//(3) [1, "test", "test2"]

2.3 add elements

var a = [1, 2, 3, 4];
a.splice(1, 0, "test");
console.log(a);//(5) [1, "test", 2, 3, 4]

3, Array filtering

The common array filtering methods in js are map(), find(), filter(), some(), every()

3.1 map()

map() does not change the original array. It constructs the return value into a new array to return.

var a= [1, 2, 3, 4];
var b = a.map((v, k) => {
	console.log("v: " + v + " k:" + k);
	return v + 1;
});
console.log(a);//(4) [1, 2, 3, 4]
console.log(b);//(4) [2, 3, 4, 5]

3.2 find()

The find() method returns the first element in the array that meets the conditions of the test function. Otherwise, it returns undefined

var a = [1, 2, 3, 4];
var b = a.find((v, k) => {
	console.log("v: " + v + " k:" + k);
	return v > 2;
});
console.log(a);//(4) [1, 2, 3, 4]
console.log(b);//3

3.3 filter()

The difference between filter() and find() is that filter forms a new array of all eligible elements.

var a = [1, 2, 3, 4];
var b = a.filter((v, k) => {
	console.log("v: " + v + " k:" + k);
	return v > 2;
});
console.log(a);//(4) [1, 2, 3, 4]
console.log(b);//(2) [3, 4]

3.4 some()

some() is to run the specified function for each item in the array. If the function returns true for any item, it returns true.

var a = [1, 2, 3, 4];
var b =  a.some((v, k) => {
	console.log("v: " + v + " k:" + k);
	return v > 2;
});
console.log(a);//(4) [1, 2, 3, 4]
console.log(b);//true

3.5 every()

Unlike some(), every() runs the specified function for each item in the array. If the function returns true for all items, it returns true. If any item returns false, it is false

var a = [1, 2, 3, 4];
var b =  a.every((v, k) => {
	console.log("v: " + v + " k:" + k);
	return v > 2;
});
console.log(a);//(4) [1, 2, 3, 4]
console.log(b);//false

4, Array inversion

Use the reverse() method to reverse the array, return the reversed array, and the original array also changes

var a = [1, 2, 3, 4];
var b = a.reverse();
console.log(a);//(4) [4, 3, 2, 1]
console.log(b);//(4) [4, 3, 2, 1]

5, Array splicing

There are two common methods of array splicing, one is to use concat() method, the other is to use es6 midpoint syntax extension operator

5.1 concat()

The concat() method does not change the original array, but returns the spliced array

var a = [1, 2];
var b = [3, 4];
var c = a.concat(b);
console.log(a);//(2) [1, 2]
console.log(b);//(2) [3, 4]
console.log(c);//(4) [1, 2, 3, 4]

5.2 es6 point syntax

var a = [1, 2];
var b = [3, 4];
a.push(...b);
console.log(a);//(4) [1, 2, 3, 4]
console.log(b);//(2) [3, 4]

6, Array extraction

slice(start, end) can be used to intercept the array, return the array elements of start and end (excluding end) as a new array, and will not change the original array. The usage is as follows:

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

7, Array to string

JSON.stringify() method can convert the array to a string (not only the number of groups, but also other objects can be converted to json string):

var a = [1, 2, 3, 4];
console.log(JSON.stringify(a));//[1,2,3,4]

8, String to array

JSON.parse Method () converts a string array to an array object:

var a = "[1,2,3,4]";
console.log(JSON.parse(a));//(4) [1, 2, 3, 4]

9, Determine whether an array contains an element

You can use for, every, some, find to determine whether an element is contained in an array. Let's not go into details here. Another method, indexOf(item, start), is used to determine whether an item element is contained in an array. Start the query from the start position (0 < = start < = length-1). If there is one, return the first occurrence position of the item element. If not, return - 1:

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

Tags: JSON

Posted on Sat, 27 Jun 2020 02:22:15 -0400 by Ironmann00