JavaScript data structure and algorithm -- array

Classification of data structure

Data structure refers to the set of data elements that have one or more relationships with each other and the relationship between data elements in the set.
Common data structures include: array, stack, linked list, queue, tree, graph, heap, hash table, etc., as shown in the figure:

array

Array is the simplest memory data structure. Array is a structure that can store multiple elements continuously in memory. The allocation in memory is also continuous. The elements in array are accessed by array subscript, which starts from 0.
tips: generally, data stores a series of values of the same data type, but in JavaScript, you can save values of different types in an array, but you don't need to use this.

1. Create array

let daysOfWeek = new Array();
let daysOfWeek = new Array(7);
let daysOfWeek = new Array('1', '2', '3', '4', '5', '6', '7');

2. Add elements

// Initialize nums array
let nums = [0,1,2,3,4,5,6];
// Add at specified location
nums[nums.length] = 7;
// Use push() to add elements to the end of the array
nums.push(8);// 0...8
nums.push(9, 10);// 0...10
// Using unshift, add the element to the top of the array
nums.unshift(-1);// -1...10
nums.unshift(-3,-2);// -3...10

3. Delete element

// pop(), delete the last
nums.pop();//-3...9
// shift(), delete the first
nums.shift();//-2...9

4. Delete or add elements anywhere

// splice() method
nums.splice(2, 3);// Delete the three numbers after index=2 - 2, - 1, 3... 9
nums.splice(2, 0, 0, 1, 2);// Insert 0, 1, 2 - 2... 9 from index=2

5.javascript array method reference

concat()  // Connect 2 or more arrays and return results
every()  // Runs the given function for each item in the array, and returns true if it returns true for each item
filter()  // Run the given function for each item in the array and return the item that can return true as the new array
forEach()  // Run the given function for each item in the array with no return value
join()  // Concatenate the incoming characters into a string
indexOf()  // Traverse from front to back, return the first index value equal to the passed in parameter, return - 1 not found
lastIndexOf()  // Traversal from the back to the front returns the first index value equal to the passed in parameter
map()  // Run the given function for each item in the array and return the result of each function call to form a new array
reverse()  // Reverse the order of elements in an array
slice()  // Pass in the index value and return the elements within the index value range of the array as a new array  
some()  // Run the given function for each item in the array, and return true if one item returns true
sort()  // Sort alphabetically, support passing in the function of the specified sorting method as the parameter
toString()  // Returns an array as a string
valueOf()  // Similar to toString, returns an array as a string

6. New method of ES6 array

@@iterator  // Returns an iterator object containing an array key value pair, which can be called synchronously to get the key value pair of an array element
copyWithin()  // Copies a series of elements in an array to the starting position specified by the array
entries()  // Returns the @ iterator containing all key value pairs of the array
includes()  // Return true if there is an element in the array, otherwise return false(es7 new)
find()  // Finds an element from the array according to the conditions given by the callback function, and returns the element if found
findIndex()  // Find the element from the array according to the conditions given by the callback function, and return the index of the element in the array if it can be found
fill()  // Fill the array with the passed in parameters
from()  // Create a new array from an existing array
keys()  // Returns the @ iterator containing all indexes of the array
of()  // Create a new array based on the parameters passed in
values()  // Returns the @ iterator containing all the values in the array

7. Advantages and disadvantages of array
Advantage:
(1) query elements by index is fast
(2) it is convenient to traverse array according to index

Disadvantages:
(1) after the size of the array is fixed, it cannot be expanded
(2) an array can only store one type of data
(3) adding and deleting are slow because other elements need to be moved.

Applicable scenarios:
Frequent queries require little storage space, and rarely increase or delete.

Keywords: Javascript

Added by biba028 on Wed, 04 Dec 2019 00:20:01 +0200