2021-05-15

数组的多种方法的总结

1.遍历数组(forEach)

var arr = ['a','b',c]

arr.forEach((item,index,arr)=>{
    console(item,index)
    // a 0   b 1  c 2
})

2.添加元素到数组的末尾(push)

被添加的数组.push(要添加的变量)

3.删除数组末尾的元素(pop)

被删除的数组.pop()
//删除数组最后一个变量

4.删除数组的前面的元素(shift)

arr.shift()
//arr为被删的数组

5.添加元素到数组的头部(unshift)

arr.unshift('d')
//arr 为添加的数组
//d   为想要添加到数最的变量

6.找出某个在数组中的索引(indexOf)

返回在数组中可以找到一个给定元素的第一个索引,如果不存在,返回-1

//语法
arr.indexOf(要查找的远素,回调函数)

7.通过索引删除摸个元素(splice)

通过删除或替换现有元素或者原地添加新的元素来修改数组,兵役数组的形式返回被手改的内容,会改变原素组

arr.splice(start,deleteCount//item)
// start  指定修改的开始位置
// deleteCount 表示要移除数组元素的个数
// item  要添加进数组的元素,从start
// 返回被删除的元素组成的一个数组,不然是一个空数组

8.用一个固定值填充一个数组(fill)

用一个固定的值填充一个数组从起始索引到终止索引内的全部元素

语法:arr.fill(value,start)   value用来填充数组元素的值

[1, 2, 3].fill(4);               // [4, 4, 4]
[1, 2, 3].fill(4, 1);            // [1, 4, 4]

9.将数组中的位置颠倒(reverse)

将数组中的位置颠倒,并返回该数组,数组中的第一个变成最后一个,最后一个变成第一个

const array1 = ['one', 'two', 'three'];


const reversed = array1.reverse();
console.log('reversed:', reversed);
// expected output: "reversed:" Array ["three", "two", "one"]

10.对数组元素进行排序(sort)

对数组的元素进行排序,并返回数组.默认将元素转换为字符串

const months = ['March', 'Jan', 'Feb', 'Dec'];
months.sort();
console.log(months);
// expected output: Array ["Dec", "Feb", "Jan", "March"]

11.由当前数组和其他若干个数组组合成新数组(concat)

用于合并两个或多个数组,不会改变现有的数组,返回一个新数组

nst array1 = ['a', 'c'];
const array2 = ['d', 'f'];
const array3 = array1.concat(array2);

console.log(array3);
// expected output: Array ["a", "c", "d", "f"]

12.判断数组是否包含一个指定的值(includes)

判断一个数组是否包含一个指定的值,有返回true,没有返回false

const array1 = [1, 2, 3];

console.log(array1.includes(2));
// expected output: true

13.连接所有数组元素组成一个字符串(join)

const elements = ['a', 'b', 'c'];

console.log(elements.join());
// expected output: "a,b,c"

14.返回一个字符串,代表数组的源代码(toSource)

var alpha = new Array("a", "b", "c");

alpha.toSource();   //返回["a", "b", "c"]

15.返回一个字符串,指定的数组及其元素(toString)

const array1 = [1, 2, 'a', '1a'];

console.log(array1.toString());
// expected output: "1,2,a,1a"

16.数组满足判断函数(every)

返回true()  或者   false()

const isBelowThreshold = (currentValue) => currentValue < 40;

const array1 = [1, 30, 39, 29, 10, 13];

console.log(array1.every(isBelowThreshold));
// expected output: true

17.数组至少有一个元素满足判断条件(some)

返回true()  或者   false()

const array = [1, 2, 3, 4, 5];

// checks whether an element is even
const even = (element) => element % 2 === 0;

console.log(array.some(even));
// expected output: true

18.将数组过滤,过滤出满足条件,返回新的数组(filter)

arr.filter(callback(element,index.array))

element:表示当前正在处理的元素

index  正在处理的元素在数组中的索引

array  数组本身

const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];

const result = words.filter(word => word.length > 6);

console.log(result);
// expected output: Array ["exuberant", "destruction", "present"]
// 过滤出数组中想要的值,返回组成一个新数组

19.找到数组中满足条件的第一个值,返回那个值(find)

没有则返回undefind

find(callback)

callback   回调函数

const array1 = [5, 12, 8, 130, 44];

const found = array1.find(element => element > 10);

console.log(found);
// expected output: 12

20.找到数组中满足提供的判断的第一个元素的索引

没有找到返回-1

const array1 = [5, 12, 8, 130, 44];

const isLargeNumber = (element) => element > 200;

console.log(array1.findIndex(isLargeNumber));
// expected output: -1

21.创建一个新数组,结果是该数组中的每个元素调用一次提供函数后的返回值(map)

arr.map(callback(value,index,array))

value  数组中正在处理的当前元素

index  数组中正在处理的当前元素的索引值

array  当前调用的数组

 var arr = [{ label: '男', value: 1 }, { label: '女', value: 0 }]
        function f(arr) {
            // 写代码,得到
         arr.map(item=>item.label)
         
        }
        var arr2 = f(arr);
        console.log(arr2);
        // arr2 = ['男', '女']     输出结果

22.方法对数组中的每个元素执行一个由您提供的reducer函数(升序执行),将其结果汇总为单个返回值。(reduce)

const array1 = [1, 2, 3, 4];
const reducer = (accumulator, currentValue) => accumulator + currentValue;

// 1 + 2 + 3 + 4
console.log(array1.reduce(reducer));
// expected output: 10
console.log(array1.reduce(reducer,5))
// expected output: 15

23.方法返回一个新的 Array Iterator 对象,该对象包含数组每个索引的值(values)

const array1 = ['a', 'b', 'c'];
const iterator = array1.values();

for (const value of iterator) {
  console.log(value);
}

// expected output: "a"
// expected output: "b"
// expected output: "c"

有遗漏的还请见谅(嘻嘻!!!)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值