为什么我要放弃javaScript数据结构与算法(第二章)—— 数组 (4)

key 方法返回包含数组索引的 @@iterator

let aKeys = number.entries(); // 得到数组索引的迭代器 console.log(aKeys.next()); // { value: 0, done: false} console.log(aKeys.next()); // { value: 1, done: false} console.log(aKeys.next()); // { value: 2, done: false}

keys方法会返回number数组的索引。一旦没有可以迭代的值,aKeys.next() 就会返回一个value属性为undefined,done属性为 true的对象。如果 done属性为false,就意味着还有可以迭代的值。

使用from方法

Array.from方法根据已有的数组创建一个新数。比如复制number数组:

let number2 = Array.from(number); number2 // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] 使用Array.of

Array根据传入的参数创建一个新数组、

let number3 = Array.of(1); let number4 = Array.of(1,2,3,4,5,6); number3 // [1] number4 // [1,2,3,4,5,6] // 复制已有的数组 let numberCopy = Array.of(...number4); numberCopy // [1,2,3,4,5,6] 使用fill方法

fill方法用静态值充填数组。

let numberCopy = Array.of(1,2,3,4,5,6); numberCopy.fill(0); // [0, 0, 0, 0, 0, 0] // 指定开始充填的索引 numberCopy.fill(2,1); // [0, 2, 2, 2, 2, 2] // 指定结束的索引(结束的索引不包括本身) numberCopy.fill(1,3,5); // [0, 2, 2, 1, 1, 2]

创建数组并初始化的时候,fill 方法就非常好用。

let ones = Array(6).fill(1); // 创建了一个长度为6,所有值都是1的数组 使用copyWithin方法

copyWithin方法复制数组中的一系列元素到同一个数组指定的起始位置。

语法:

array.copyWithin(target, start, end) 参数 描述
target   必需。复制到指定目标索引位置。  
start   可选。元素复制的起始位置。  
end   可选。停止复制的索引位置 (默认为 array.length)。如果为负值,表示倒数。  
let copyArray = [1,2,3,4,5,6]; copyArray.copyWithin(0,3); // [4, 5, 6, 4, 5, 6] let copyArray1 = [1,2,3,4,5,6]; copyArray1.copyWithin(1,3,5); // [1, 4, 5, 4, 5, 6] 排序元素

反序输出最开始的长度为15的number数组。

number.reverse(); // [15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

尝试使用JavaScript自带的排序函数 sort();

number.sort(); //[1, 10, 11, 12, 13, 14, 15, 2, 3, 4, 5, 6, 7, 8, 9]

跟我们预期的有些不一样,这是因为 sort 方法在对数组进行排序 的时候,把元素默认成字符串进行相互比较。所以我们要自己定义一个比较函数。

number.sort((a,b) =>{ return a -b; }) // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

上述代码,如果 b 大于 a,会返回负数,反之就是正数。如果相等的话,就会返回0。下面的写法会清晰一点

function compare(a, b){ if(a < b){ return -1; } if(a > b){ return 1; } return 0; } number.sort(compare);

sort 方法接受 compareFunction作为参数来比较元素。然后sort 会用它来排序数组

自定义排序

我们可以对任何对象类型的数组排序,也可以创建 compareFuntion 来比较元素。例如对象 Person 有名字和属性,我们希望根据年龄排序。

var friends = [ {name: 'John', age: 30}, {name: 'Ana', age: 20}, {name: 'Chris', age: 25} ]; friends.sort((a, b) =>{ return a.age - b.age; }) // {name: "Ana", age: 20} {name: "Chris", age: 25} {name: "John", age: 30}

字符串排序

var names = ['Ana', 'ana', 'John', 'john']; names.sort(); // ["Ana", "John", "ana", "john"]

字符串的比较是根据对应的 ASCⅡ值来比较的。例如 A、J、a、j对应分别是65、74、97、106。

虽然字母表的 a 是排靠前的,但是由于 ASCⅡ值 比较大,所以没有排在首位。如果忽略大小写呢?会是怎么样

names.sort((a, b) =>{ if(a.toLowerCase() < b.toLowerCase()){ return -1; } if(a.toLowerCase() > b.toLowerCase()){ return 1; } return 0; }) // ["Ana", "ana", "John", "john"] 搜索

搜索有两个方法:indexOf方法返回与参数匹配的第一个元素的索引,lastIndexOf返回与参数匹配的最后一个元素的索引。

number.indexOf(10); // 9 number.indexOf(100); // -1 (因为100不存在数组里面) number.puhs(10); number.lastIndexOf(10); // 15 number.lastIndexOf(100) // -1 ES6 find 和 findIndex方法 let number = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]; const multipleof13 = (element, index, array) => { return (element % 13 == 0); } number.find(multipleof13); //13 number.findIndex(multipleof13); // 12

find 和 findIndex方法接受一个回调函数,搜索一个满足回调函数条件的值。上面的例子中,我们要从数组里找有个13的倍数。

ES7 使用includes方法

如果数组存在某个元素,includes 方法就会返回 true,否则就返回 false。

number.includes(15) // true number.includes(20) // false number.includes(4,4) // false 第二个参数为开始搜索的索引 输出字符串

toString 和 jion 方法

如果想把数组里所有元素输出位一个字符串,可以使用 toString 方法

number.toString(); // "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15"

还可以用不同的分隔符将元素隔开

number.join('-'); // "1-2-3-4-5-6-7-8-9-10-11-12-13-14-15" 类型数组

JavaScript由于不是强类型的,因此可以存储任意类型的数据,而类型数组则用于存储单一的数据。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zgfwyw.html