Home  >  Article  >  Web Front-end  >  Introduction to operations related to arrays of JavaScript data structures (with examples)

Introduction to operations related to arrays of JavaScript data structures (with examples)

不言
不言forward
2019-02-13 09:40:342116browse

This article brings you an introduction to the related operations of arrays of JavaScript data structures (with examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Classification of data structures

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

Introduction to operations related to arrays of JavaScript data structures (with examples)

## Array

Array is the simplest memory data structure. An array is a structure that can store multiple elements continuously in memory. The allocation in memory is also continuous. The elements in the array are indexed by the array subscript. For access, array subscripts start from 0.

tips: Data generally stores a series of values ​​of the same data type, but in JavaScript, values ​​of different types can be saved in an array, but this is generally not needed.

1.Create an array

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

// 初始化nums数组
let nums = [0,1,2,3,4,5,6];
// 指定位置添加
nums[nums.length] = 7;
// 使用push(),把元素添加到数组末尾
nums.push(8);// 0...8
nums.push(9, 10);// 0...10
// 使用unshift,把元素添加到数组首位
nums.unshift(-1);// -1...10
nums.unshift(-3,-2);// -3...10
3.Delete elements

// pop(),删除最后一个
nums.pop();//-3...9
// shift(),删除第一个
nums.shift();//-2...9
4.Delete or add elements at any position

// splice()方法
nums.splice(2, 3);// 删除 index=2 开始的后的3个数 -2,-1,3...9
nums.splice(2, 0, 0, 1, 2);// 从index=2开始插入0,1,2  -2...9
5.javascript array method reference

concat()  // 连接2个或多个数组,并返回结果
every()  // 对数组中的每一项运行给定函数,如果该函数对每一项都返回true,则返回true
filter()  // 对数组中的每一项运行给定函数,返回该函数能返回true的项作为新数组
forEach()  // 对数组中的每一项运行给定函数,没有返回值
join()  // 按传入的字符连接成一个字符串
indexOf()  // 从前往后遍历,返回第一个与传入参数相等的索引值,没找到返回-1
lastIndexOf()  // 从后往前遍历,返回第一个与传入参数相等的索引值
map()  // 对数组中的每一项运行给定函数,返回每次函数调用的结果组成新的数组
reverse()  // 颠倒数组中元素的顺序
slice()  // 传入索引值,将数组对应索引值范围内的元素作为新数组返回  
some()  // 对数组中的每一项运行给定函数,如果某一项返回true,则返回true
sort()  // 按照字母顺序排序,支持传入指定排序方法的函数作为参数
toString()  // 将数组作为字符串返回
valueOf()  // 和toString类似,将数组作为字符串返回
6.ES6 new array method

@@iterator  // 返回一个包含数组键值对的迭代器对象,可通过同步调用得到数组元素的键值对
copyWithin()  // 复制数组中一系列元素,到该数组指定的起始位置
entries()  // 返回包含数组所有键值对的@@iterator
includes()  // 数组中存在某个元素则返回true,否则返回false(es7新增)
find()  // 根据回调函数给定的条件从数组中查找元素,如果找到则返回该元素
findIndex()  // 根据回调函数给定的条件从数组中查找元素,如果能找到就返回该元素在数组中的索引
fill()  // 用传入参数填充数组
from()  // 根据已有数组创建一个新数组
keys()  // 返回包含数组所有索引的@@iterator
of()  // 根据传入的参数创建一个新数组
values()  // 返回包含数组中所有值的@@iterator
7.Array advantages and disadvantages

Advantages:
(1) Query by index The element speed is fast
(2) It is convenient to traverse the array according to the index

Disadvantages:

(1) After the size of the array is fixed, it cannot be expanded
(2) The array can only store one type Data
(3) Adding and deleting operations are slow because other elements need to be moved.

Applicable scenarios:

Frequent queries, small storage space requirements, and few additions and deletions.

The above is the detailed content of Introduction to operations related to arrays of JavaScript data structures (with examples). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete