This is the 14th day of my participation in Gwen Challenge

An array of

Array: Represents an ordered collection of data, with an order between each item

Arrays use literals: [], separated by commas. Each item can be of any data type. We usually write the same data.

The last item does not contain a comma.

Array data type: Object (reference data)

Read: Array name [index] Reads the value of each item in the array by index value. Index starts at 0.

console.log(arr1[0]);
Copy the code

Setting: Assign with =

arr1[0] = 100;
Copy the code

Length attribute

Array Length property: Represents the total number of data stored in the array

Object.method ()

Object. Properties

Read: array.length

arr1.length
Copy the code

The last index of the array is arr. Length-1

arr1[arr1.length - 1]
Copy the code

You can force an array to be stretched by index assignment

// Lengthen the array by assigning index values
arr1[10] = 10;
console.log(arr1.length);
// undefined is not assigned
console.log(arr1[9]);
Copy the code

You can force an array to be stretched by length, and undefined if no assignment is made

// The array can be stretched by length
arr1.length = 20;
console.log(arr1.length);
// Undefined without an assignment
console.log(arr1[18]);
Copy the code

Array traversal

Array each item in an array

// Each item can be a different data type
var arr = [10.Infinity."Hello".true.function(){console.log(1); }];// Output the value of each item in the array. The index value ranges from 0 to arr. Length -1;
for(var i = 0; i <= arr.length - 1; i ++) {
	console.log(arr[i]);
}
Copy the code

Array methods

Head to tail operation method

Push (): Adds data to the end of the array

Parameters: Data to be added, separated by commas if multiple

Return value: The length of the new array

var arr = [1.2.3.4.5.6];
// Push () adds at the end of the array
// Parameter: the data to be added, can be a hash value. Or a literal, a variable name
// Returns the new array length
console.log(arr.push(77[90.100]));
// Push () the original array method changes
console.log(arr);
Copy the code

Pop (): deletes the last item of the array

Parameters: do not write

Return value: The value of the deleted item

Unshift (): Adds at the beginning of the array

Parameters: Data to add

Return value: The length of the new array

Shift (): Deletes the first item in the array

Parameters: do not write

Return value: Deleted value

Merge and split methods

Merger: concat ()

Parameter: The array to merge

Return value: New array after merge

var arr1 = [1.2.3.4.5.6];
var arr2 = [7.8.9];
// concat() 
// Return value: the new array after merging
console.log(arr1.concat(arr2));
// The original array does not change
console.log(arr1);
Copy the code

Split: slice (start, end)

Parameters: Two parameters are required

Start: indicates the index value at the beginning of an array interception

End: indicates the index value at the end of an array interception

Include start but not end

Return value: truncated new array

console.log(arr1.slice(2.5));
// The original array does not change
console.log(arr1);
Copy the code

You can also use negative numbers, which are reciprocal, starting at -1

Array methods

Array delete, insert, replace method

Splice (index, howmany, elements…)

Index: must be written, delete the entry start index value.

Howmany: Must write, delete the number of array items, if not delete write 0

Elements: Elements can be omitted and separated by commas. Insert, replace element

Return value: Deletes a new array of items, returns an empty array without deletion.

Delete: howmany cannot be 0,elements should not be written

/ / array
var arr = [1.2.3.4.5.6.7];
/ / delete
var arrNew = arr.splice(2.3);
// The original array changes
console.log(arr);
/ / the return value
console.log(arrNew);
Copy the code

Insert: howmany: write as 0, elements write the data to be inserted

/ / insert,2,3,333,444,4,5,6,7 [1]
console.log(arr.splice(3.0.333.444));
console.log(arr);
Copy the code

Replace: Howmany cannot be 0, and elements indicates the replaced data

/ / replace,2,333,444,555,5,6,7 [1]
console.log(arr.splice(2.2.333.444.555));
console.log(arr);
Copy the code

Reverse order and sort

Reverse: Reverse (), which simply reverses each of the first and last items of the array backwards and forwards

Parameter: omitted

Return value: new array inverted

/ / array
var arr = [1.2.3.4.5.6.7];
/ / reverse
console.log(arr.reverse());
// The original array changes
console.log(arr);
Copy the code

Sorting: sort ()

Parameter: omitted

Return value: new sorted array

Sort by converting each item in the array to a string, and then comparing it by character encoding, the later the encoding, the larger the value. Numbers Uppercase letters Lowercase letters

/ / array
var arr1 = [123.37."a"."B".true];
console.log(arr1.sort());
// The original array changes
console.log(arr1);
Copy the code

Parameter: can write the comparison function, the comparison function is an anonymous function, can declare two parameters, a,b

A and B represent two data points to be compared

You can customize ascending (descending) :

Take ascending order as an example: A > b: returns a negative number

A == b: returns 0

A < b: returns a positive number

// Custom ascending order
arr2.sort(function(a,b) {
	if(a > b) {
		return 2;
	}else if(a == b) {
		return 0;
	}else if(a < b) {
		return -1; }});Copy the code

join()

Represents a method that converts an array to a string

Parameter: indicates that each item is connected according to the parameter

Return value: a string

var arr = [1.2.3.4.5.6.7];
// Omit the argument to indicate a comma connection
// Return value new string
// The original array does not change
console.log(arr.join());
console.log(typeof arr.join());
console.log(arr);

// Write parameter: indicates parameter connection
console.log(arr.join("+"));
Copy the code

The string length

A string also has a Length attribute, which indicates the number of characters, including letters, digits, punctuation, Spaces, and Chinese characters

var str = "Today is September, the temperature is low!";
console.log(str.length);   / / 12
Copy the code

String method

charAt

Can go to the specified position character

Parameter: index value (index starts at 0)

Return value: character

for(var i = 0 ; i < str.length ; i ++) {
	// I indicates the index value
	console.log(str.charAt(i));
}
Copy the code

indexOf()

The result is the index of the specified character. No modified character returns the value -1

Parameter: character

Return value: index value

// indexOf() returns the specified character position
console.log(str.indexOf("September"));
// Returns -1 without the character
console.log(str.indexOf("August"));
Copy the code

concat()

For string merging

Parameter: String to be merged

Return value: the new string after merging

str2.concat(str1)
Copy the code

slice()

Used for string interception

Parameters: the first parameter start, the index value of the intercept item, the second parameter end, the index value of the end of the intercept item, including start but not end

Return value: truncated string

// Slice (start,end) string interception
var str3 = "012345678";
// Include the start index but not the end index
console.log(str3.slice(3.6));
// You can also use negative numbers to write from small to large
console.log(str3.slice(-6, -3));
// You can also omit the end to indicate the interception to the end
console.log(str3.slice(-6));
Copy the code

substr()

Used for string interception

Parameter: the first parameter: index, the start item index value; The second parameter howmany: the number of intercepts

Return value: truncated string

// substr(index,howmany)
var str4 = "01234567";
console.log(str4.substr(3.3));
// howmany can be omitted to indicate interception to the end
console.log(str4.substr(3));
Copy the code

substring()

Used for string interception

Parameter: the first parameter: start, the start item index value; The second argument end: the end index value of the interceptor

Return value: truncated string

Different from slice: Start and end can be written in any way, indicating that small index values are truncated to large index values, excluding large index values

// substring(), the index value can be size insensitive
var str5 = "01234567";
console.log(str5.substring(3.6));
// Does not include large index values
console.log(str5.substring(6.3));
Copy the code

split()

Convert a string to an array

Parameter: String to cut

Return value: an array of cut strings

var str = "abcxayuxac";
console.log(str.split("x"));
Copy the code

toUpperCase()

Converts the string to uppercase

Parameter: omitted

Return value: converted new string

str.toUpperCase()
Copy the code

toLowerCase()

Convert to lowercase

Parameter: omitted

Return value: converted string

tr2.toLowerCase()
Copy the code

Example: Capitalize the first letter of each word “everything is good in its season”. “Everything Is Good In Its Season.”

/* Capitalize "everything is good in its season". Everything = E + verything 3 turn the array into a string */

// Convert string to array split()
var str = "everything is good in its season";
var arr = str.split("");
// Reassign each item of the array
for(var i = 0 ; i < arr.length ; i ++) {
	Verything = E + verything
	arr[i] = arr[i].charAt(0).toUpperCase() + arr[i].slice(1); 
}
// Convert an array to a string
str = arr.join("");
console.log(str);
Copy the code