Loading

数组方法

04、数组的方法

4.1 - sort 排序

可以使用数组自带的sort方法,它的参数是一个函数,可以接受两个参数进行比较大于0,进而确定升序还是降序

 const months = ['March', 'Jan', 'Feb', 'Dec'];
 months.sort();
 console.log(months);
 // expected output: Array ["Dec", "Feb", "Jan", "March"]
 ​
 const array1 = [1, 30, 4, 21, 100000];
 array1.sort();
 console.log(array1);
 // expected output: Array [1, 100000, 21, 30, 4]

4.2 - join 转字符串

join方法将一个数组(或一个类数组对象)的所有元素连接成一个字符串并返回这个字符串。如果数组只有一个项目,那么将返回该项目而不使用分隔符。

 const elements = ['Fire', 'Air', 'Water'];
 ​
 console.log(elements.join());
 // expected output: "Fire,Air,Water"
 ​
 console.log(elements.join(''));
 // expected output: "FireAirWater"
 ​
 console.log(elements.join('-'));
 // expected output: "Fire-Air-Water"

4.3 - reverse 颠倒

颠倒数组中元素的顺序。 改变数组

 const array1 = ['one', 'two', 'three'];
 console.log('array1:', array1);
 // expected output: "array1:" Array ["one", "two", "three"]
 ​
 const reversed = array1.reverse();
 console.log('reversed:', reversed);
 // expected output: "reversed:" Array ["three", "two", "one"]
 ​
 // Careful: reverse is destructive -- it changes the original array.
 console.log('array1:', array1);
 // expected output: "array1:" Array ["three", "two", "one"]

4.4 - concat 合并

方法用于合并两个或多个数组。此方法不会更改现有数组,而是返回一个新数组。

 const array1 = ['a', 'b', 'c'];
 const array2 = ['d', 'e', 'f'];
 const array3 = array1.concat(array2);
 ​
 console.log(array3);
 // expected output: Array ["a", "b", "c", "d", "e", "f"]

4.5 - slice 剪切

slice()方法返回一个新的数组对象,这一对象是一个由 begin 和 end 决定的原数组的浅拷贝(包括 begin,不包括end)。原始数组不会被改变。

 const animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];
 ​
 console.log(animals.slice(2));
 // expected output: Array ["camel", "duck", "elephant"]
 ​
 console.log(animals.slice(2, 4));
 // expected output: Array ["camel", "duck"]
 ​
 console.log(animals.slice(1, 5));
 // expected output: Array ["bison", "camel", "duck", "elephant"]
 ​
 console.log(animals.slice(-2));
 // expected output: Array ["duck", "elephant"]
 ​
 console.log(animals.slice(2, -1));
 // expected output: Array ["camel", "duck"]

4.6 - splice 改变数组

添加或删除数组中的元素,会改变原始数组。

 const months = ['Jan', 'March', 'April', 'June'];
 months.splice(1, 0, 'Feb');
 // inserts at index 1
 console.log(months);
 // expected output: Array ["Jan", "Feb", "March", "April", "June"]
 ​
 months.splice(4, 1, 'May');
 // replaces 1 element at index 4
 console.log(months);
 // expected output: Array ["Jan", "Feb", "March", "April", "May"]

4.7 - push 末尾添加

向数组的末尾添加一个或多个元素,并返回新的长度

 const animals = ['pigs', 'goats', 'sheep'];
 ​
 const count = animals.push('cows');
 console.log(count);
 // expected output: 4
 console.log(animals);
 // expected output: Array ["pigs", "goats", "sheep", "cows"]
 ​
 animals.push('chickens', 'cats', 'dogs');
 console.log(animals);
 // expected output: Array ["pigs", "goats", "sheep", "cows", "chickens", "cats", "dogs"]

4.8 - pop 末尾删除

删除数组的最后一个元素并返回删除的元素。

 const plants = ['broccoli', 'cauliflower', 'cabbage', 'kale', 'tomato'];
 ​
 console.log(plants.pop());
 // expected output: "tomato"
 ​
 console.log(plants);
 // expected output: Array ["broccoli", "cauliflower", "cabbage", "kale"]
 ​
 plants.pop();
 ​
 console.log(plants);
 // expected output: Array ["broccoli", "cauliflower", "cabbage"]

4.9 - shift 首部添加

把数组的第一个元素从其中删除,并返回第一个元素的值

 const array1 = [1, 2, 3];
 ​
 const firstElement = array1.shift();
 ​
 console.log(array1);
 // expected output: Array [2, 3]
 ​
 console.log(firstElement);
 // expected output: 1

4.10 - unshift 首部删除

向数组的开头添加一个或更多元素,并返回新的长度

 const array1 = [1, 2, 3];
 ​
 console.log(array1.unshift(4, 5));
 // expected output: 5
 ​
 console.log(array1);
 // expected output: Array [4, 5, 1, 2, 3]
 ​

4.11 - filter 过滤

创建一个新数组, 其包含通过所提供函数实现的测试的所有元素。

返回 true 表示该元素通过测试,保留该元素,false 则不保留。

 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"]

4.12 - find 查找

数组中第一个满足所提供测试函数的元素的值,否则返回 undefined。

 const array1 = [5, 12, 8, 130, 44];
 ​
 const found = array1.find(element => element > 10);
 ​
 console.log(found);
 // expected output: 12

4.13 - forEach 遍历

 const array1 = ['a', 'b', 'c'];
 ​
 array1.forEach(element => console.log(element));
 ​
 // expected output: "a"
 // expected output: "b"
 // expected output: "c"

4.14 - includes 包含

includes() 方法用来判断一个数组是否包含一个指定的值,根据情况,如果包含则返回 true,否则返回 false。

 const array1 = [1, 2, 3];
 ​
 console.log(array1.includes(2));
 // expected output: true
 ​
 const pets = ['cat', 'dog', 'bat'];
 ​
 console.log(pets.includes('cat'));
 // expected output: true
 ​
 console.log(pets.includes('at'));
 // expected output: false

4.15 - isArray 判断数组

Array.isArray() 用于确定传递的值是否是一个 Array。

 Array.isArray([1, 2, 3]);
 // true
 Array.isArray({foo: 123});
 // false
 Array.isArray("foobar");
 // false
 Array.isArray(undefined);
 // false

4.16 - map 遍历

map 方法会给原数组中的每个元素都按顺序调用一次 callback 函数。callback 每次执行后的返回值(包括 undefined)组合起来形成一个新数组

 const array1 = [1, 4, 9, 16];
 ​
 // pass a function to map
 const map1 = array1.map(x => x * 2);
 ​
 console.log(map1);
 // expected output: Array [2, 8, 18, 32]

4.17 - reduce 遍历

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

 const array1 = [1, 2, 3, 4];
 const reducer = (previousValue, currentValue) => previousValue + currentValue;
 ​
 // 1 + 2 + 3 + 4
 console.log(array1.reduce(reducer));
 // expected output: 10
 ​
 // 5 + 1 + 2 + 3 + 4
 console.log(array1.reduce(reducer, 5));
 // expected output: 15

4.18 - some 判断

some()方法测试数组中是不是至少有1个元素通过了被提供的函数测试。它返回的是一个Boolean类型的值。

some()为数组中的每一个元素执行一次 callback 函数,直到找到一个使得 callback 返回一个“真值”(即可转换为布尔值 true 的值)。如果找到了这样一个值,some() 将会立即返回 true。否则,some() 返回 false。callback 只会在那些”有值“的索引上被调用,不会在那些被删除或从来未被赋值的索引上调用。

 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

4.19 - toString 转字符串

 const array1 = [1, 2, 'a', '1a'];
 ​
 console.log(array1.toString());
 // expected output: "1,2,a,1a"
posted @ 2021-11-16 00:40  菜鸟的道路  阅读(71)  评论(0)    收藏  举报