Javascript array usage(multiple examples, with hidden define, add a property,delete,sort and concat)

Lionsure 2020-06-05 Original by the website

Javascript array operations include define, add, delete, read, modify, copy, sort, slice and concat, and convert all elements of the array into strings. There are two ways to define an array, one is to create a new array, and the other is an implicit definition, which is determined according to the assigned value. There are three methods for adding and removing elements: push(), unshift(), splice(), pop(), shift(), and splice().

After defining a Javascript array, you can add a property to it and sort the elements of array; sort is divided into ascending and descending order, using the sort() and reverse() methods, respectively. Two arrays can be merged into one, an array and a string of characters can also be merged into an array, and implemented with the concat() method; in addition, the slice() method can be used to extract a specified number of array elements. Array can be converted to string with the join(), toString(), and toLocaleString() methods.

Js multidimensional array has two definition methods, one is new Array(new Array(), new Array(), ...), and the other is [[,, ...], [,, ...] , ...].

 

1. Javascript define array

var array_object = new Array(); // Define an array

var array_object = new Array([size]); // Define an array of specified length

var array_object = new Array([element0[, element1[, ...[, elementN]]]]); // Define an array and assign a value

 

Examples:

var array_object1 = new Array(); // Define an empty array

var array_object2 = new Array(12); // Define a 12-element array

var array_object3 = new Array (1,2,3,4,5); // Define an array and initialize with 5 elements

or var array_object3 = ["a","b","c","d","e"]; // Hidden declaration

 

2. Javascript add element to array

array_object.push([item1 [item2 [...[itemN ]]]]); // Add one or more new elements to the end of the array, and return the new length of the array

array_object.unshift([item1 [item2 [...[itemN ]]]]); // Add one or more new elements to the front of the array, the elements in the array are automatically moved backward, and the new length of the array is returned.

array_object.splice(start,deleteCount,[item1[, item2[, ...[,itemN]]]]); // Delete the number of elements that are specified by deleteCount from the start position, then insert item1, item2, ..., itemN from the position, and return the elements to be deleted.

 

Examples:

var num_lists = new Array (10,11,12,13,14);

num_lists.push(15); // Result: 10,11,12,13,14,15

num_lists.unshift(15); // Result: 15,10,11,12,13,14

 

num_lists.splice(2,0,15); // Result: 10,11,15,12,13,14

var new_array = num_lists.splice(2,2,15); // Result: num_lists: 10,11,15,14; new_array: 12,13

 

a[100000] = 20; // Assign a value with the index far greater than its length

 

3. Javascript delete element from array

array_object.pop(); // Remove the last element and return the value of the element, or return undefined if the array is empty

array_object.shift(); // Remove the first element and return the value of the element, the remaining elements are automatically shifted forward

array_object.splice(start, deleteCount); // Delete the number of elements that are specified by deleteCount from the specified position(start), and return the removed elements in an array.

 

Example:

var letters = new Array("a","b","c","d","e");

var new_arr1 = letters.pop(); // Result: letters: a,b,c,d; new_arr1: e

var new_arr2 = letters.shift(); // Result: letters: b,c,d,e; new_arr2: a

var new_arr3 = letters.splice(2,2); // Result: letters: a,b,e;  new_arr3: c,d

 

4. Read and modify array element(Javascript change value of array element)

var lists = new Array(10,11,12,13,14);

var a = lists[1]; // Read value of first element in array, result: a: 11

var b = lists[1] = 15; // Change value of first element in array, result: lists: 10,15,12,13,14; b: 15

 

5. Create property in javascript

var person = new Array();

person.name = "Jaimer Robert";// Create a "name" property

person.age = 28; // Create a "age" property

 

6. Javascript array sort

array_object.sort(); // Sort the elements of an array in place and return address of the sorted array

array_object.reverse(); // Sort the elements of an array in place in reverse order and return address of the sorted array

 

Example:

var number_array = [6,7,9,5,3,2,1];

number_array.sort(); // Sort array of numbers, result: number_array: 1,2,3,5,6,7,9

 

var letter_array = ["d", "b", "c", "a", "e"];

letter_array.sort(); // Sort array of objects, result: letter_array: a,b,c,d,e

 

var number_array = new Array(1,2,3,4,5);

number_array.reverse(); // Sort the array of numbers in place in reverse order, result: 5,4,3,2,1

 

var letter_array = new Array("d", "b", "c", "a", "e");

letter_array.reverse(); // Sort array of objects in place in reverse order, result: e,a,c,b,d. Note: The result is error.

 

7. Javascript array slice and concat

arr_obj.slice(start, [end]); // Returns a part of the array from "start" to "end" as an array, excluding the element corresponding to "end"; if "end" is omitted, all elements after "start" are returned

arr_obj.concat([item1[, item2[, ... [,itemN]]]]); // Merge multiple arrays(or strings, a mixture of arrays and strings) into one array, and return the merged new array

 

Example:

var arr = new Array (1,2,3,4,5,6,8,9);

var new_arr = arr.slice(1,5); // Result: new_arr: 2,3,4,5

 

var b = [10, 11, 12];

var c = arr.concat(b); // Result: c: 1,2,3,4,5,6,8,9,10,11,12

var d = arr.concat("ab"); // Result: d: 1,2,3,4,5,6,8,9,ab

 

8. Convert array of characters to string in javascript

arr_obj.join(separator); // Return the string after converting the array elements into characters, separated by "separator".

arr_obj.toString(); // Convert the array to a string and return the string

arr_obj.toLocaleString(); // Convert the array to a local string. Similar to toString(), exactly the same in FireFox; under ie, if it is a number, it will be followed by two decimal places; if it is a string, a space will be added after ",".

 

Example:

var arr = new Array(1,2,3,4,5);

var a = arr.join(""); // Result: a: 12345;

var b = arr.join("-"); // Result: b: 1-2-3-4-5;

 

var c = arr.toString(); Result: c: 1,2,3,4,5;

var d = arr.toLocaleString(); // Result: d(ie): 1.00, 2.00, 3.00, 4.00, 5.00, 6.00; Chrome or Firefox: 1,2,3,4,5

 

var arr = new Array(); arr[0] = "ab"; arr[1] = "cde"; arr[2] = "fg";

var str = arr.toLocaleString(); // Result: str: ab,cde,fg;

 

9. Javascript create matrix array

(1) Method one

var matrix = [
  [1, 2, 3],
  [4, 5, 6],
  ["a", "b", "c"]
   ];

matrix1[2][2] //Return c

(2) Method two

var matrix = new Array(
  new Array(1, 2, 3),
  new Array(5, 6, 7),
  new Array("a", "b", "c")
   );

matrix1[2][2] //Return c