数组方法

168 阅读1分钟

常用的数组方法

1.push pop unshift shift sort reverse slice splice

2.concat join

3.some any filter

方法分类

修改原数组 - 数组中添加元素

1.push: 末尾插入,返回数组长度

2.unshift: 头部插入,返回数组长度

3.splice: 修改原数组,参数分别是起始位置、删除的个数、要插入的元素,返回值是删除的数组

修改原数组 - 数组中删除元素

1.pop: 末尾删除,返回删除的元素 2.shift: 头部删除,返回删除的元素 3.splice: 修改原数组,返回的是删除的是删除的参数数组

栈的数据结构特征是先进后出,底层可以用数组实现,数组的push添加元素,pop方法删除元素

简单实现

const stack = []

stack.push(1)
stack.push(2)
stack.push(3)
stack.push(4)

// 从栈中取出数据办法

while(stack.length) {
    const item = stack.pop()
    console.log('现在出栈的是------', item)
}

队列

队列是先进先出的数据结构,底层使用数组实现,使用数组的push新增元素,shift方法删除元素

简单实现

const stack = []

stack.push(1)
stack.push(2)
stack.push(3)
stack.push(4)

// 从栈中取出数据办法

while(stack.length) {
    const item = stack.shift()
    console.log('现在出栈的是------', item)
}