基于ASP.NET的购物网站建设,网业打开慢的原因,做经销找厂家好的网站,论医院网站的建设JavaScript高级程序设计(第4版)读书分享笔记记录 适用于刚入门前端的同志 除了 Object#xff0c;Array 应该就是 ECMAScript 中最常用的类型了。
创建数组
使用 Array 构造函数
在使用 Array 构造函数时#xff0c;也可以省略 new 操作符。
let colors new Array()
let … JavaScript高级程序设计(第4版)读书分享笔记记录 适用于刚入门前端的同志 除了 ObjectArray 应该就是 ECMAScript 中最常用的类型了。
创建数组
使用 Array 构造函数
在使用 Array 构造函数时也可以省略 new 操作符。
let colors new Array()
let colors new Array(red, blue, green);let colors Array(3); // 创建一个包含 3 个元素的数组
let names Array(Greg); // 创建一个只包含一个元素即字符串Greg的数组
数组字面量表示法
let colors [red,blue,green]
let values [1,2,3]
Array 构造函数还有两个 ES6 新增的用于创建数组的静态方法from()和 of()。
Array.from()
JavaScript from() 方法 | 菜鸟教程 Array.from()的第一个参数是一个类数组对象即任何可迭代的结构或者有一个 length 属性 和可索引元素的结构。let arr Array.from(Matt)
console.log(arr)//[M,a,t,t]// Array.from()对现有数组执行浅复制
const a1 [1, 2, 3, 4];
const a2 Array.from(a1);
console.log(a1); // [1, 2, 3, 4]
console.log(a2); // [1, 2, 3, 4]
alert(a1 a2); // false Array.of() Array.of()可以把一组参数转换为数组。 let arr Array.of(1,2,3,4)
console.log(arr,arr) // [1,2,3,4] 数组索引 要取得或设置数组的值需要使用中括号并提供相应值的数字索引
let colors [red, blue, green]; // 定义一个字符串数组
alert(colors[0]); // 显示第一项
colors[2] black; // 修改第三项
colors[3] brown; // 添加第四项
cosnole.log(colors) //[red, blue, black,brown]
检测数组
if (value instanceof Array){ // 操作数组
}if (Array.isArray(value)){ // 操作数组
}
迭代器方法
keys()
keys()返回数组索引的迭代器const a [foo, bar, baz, qux];
const akeys a.keys()
for(let arr of akeys){console.log(arr) // 0 1 2 3
}values()
values()返回数组元素的迭代器const a [foo, bar, baz, qux];
const avalues a.values()
for(let arr of avalues){console.log(arr) // foo bar baz qux
}entries() entries()返回索引/值对的迭代器const a [foo, bar, baz, qux];
const aentries a.entries()
for(let [index,item] of aentries){console.log(index,item)
}//0 foo
//1 bar
//2 baz
//3 qux复制和填充方法 ES6 新增了两个方法批量复制方法 copyWithin()以及填充数组方法 fill()。 JavaScript fill() 方法 | 菜鸟教程 JavaScript copyWithin() 方法 | 菜鸟教程 转换方法 toString let colors [red, blue, green]; // 创建一个包含 3 个字符串的数组
console.log(colors.toString()); // red,blue,green valueOf() let colors [red, blue, green]; // 创建一个包含 3 个字符串的数组
console.log(colors.valueOf())//[red, blue, green] join() let colors [red, green, blue];
console.log(colors.join(,)); // red,green,blue
console.log(colors.join(||)); // red||green||blue 栈方法 栈是一种后进先出LIFOLast-In-First-Out的结构也就是最近添加的项先被删除. push() push()方法接收任意数量的参数并将它们添加到数组末尾返回数组的最新长度。 let colors new Array(); // 创建一个数组
let count colors.push(red, green); // 推入两项
console.log(count,count)// 2
console.log(colors) [red, green] pop() pop()方法则用于删除数组的最后一项同时减少数组的 length 值返回被删除的项。 let colors [red,green,bule]
let item colors.pop(); // 取得最后一项
console.log(item,item) //bule
console.log(colors) [red,green] 队列方法 队列以先进先出FIFOFirst-In-First-Out形式限制访问 shift() 它会删除数组的第一项并返回它然后数组长度减 1。 let colors [red,green,bule]
let item colors.shift(); // 取得最后一项
console.log(item,item) //red
console.log(colors) //[green,bule] unshift() 在数组开头添加任意多个值然后返回新的数组长度。 let colors [bule]; // 创建一个数组
let count colors.unshift(red, green);
console.log(count,count) // 3
console.log(colors,colors) //[red, green,bule]排序方法 数组有两个方法可以用来对元素重新排序reverse()和 sort()。 reverse() 是将数组元素反向排列 let values [1, 2, 3, 4, 5];
values.reverse();
console.log(values,values) //[5,4,3,2,1] sort() sort()会按照升序重新排列数组元素即最小的值在前面最大的值在后面。 sort()会在每一项上调用 String()转型函数然后比较字符串来决定顺序。即使数组的元素都是数值也会先把数组转换为字符串再比较、排序。以下代码示例我们希望的是值升序排列为 0,1,5,10,15但是用sort排序后得到却是[0,1,10,15,5] 就是因为sort()会在每一项上调用 String()转型函数然后比较字符串来决定顺序 let values [0, 1, 5, 10, 15];
values.sort();
console.log(values); // [0,1,10,15,5] 为此sort()方法可以接收一个比较函数用于判断哪个值应该排在前面。 比较函数接收两个参数 升序 如果第一个参数应该排在第二个参数前面就返回负值 如果两个参数相等就返回 0 如果第一个参数应该排在第二个参数后面就返回正值。 function compare(value1, value2) { if (value1 value2) { return -1; } else if (value1 value2) { return 1; } else { return 0; }
}
let values [0, 1, 5, 10, 15];
values.sort(compare);
console.log(values); // 0,1,5,10,15 降序 function compare(value1, value2) { if (value1 value2) { return 1; } else if (value1 value2) { return -1; } else { return 0; }
}
let values [0, 1, 5, 10, 15];
values.sort(compare);
alert(values); // 15,10,5,1,0 如果数组的元素是数值可以用两数相减就能排序 function compare(value1, value2){ return value2 - value1;
}let values [2,4,1,3,6,5]
values.sort(compare)
console.log(values) //[6,5,4,3,2,1]function compare(value1, value2){ return value1 - value2;
}let values [2,4,1,3,6,5]
values.sort(compare)
console.log(values) //[1,2,3,4,5,6] 操作方法 concat() concat()方法可以在现有数组全部元素基础上创建一个新数组,最后返回这个新构建的数组,不改变原数组 let colors [red, green, blue];
let colors2 colors.concat(yellow, [black, brown]);
console.log(colors); // [red, green,blue]
console.log(colors2); // [red, green, blue, yellow, black, brown] slice() slice()用于创建一个包含原有数组中一个或多个元素的新数组。slice()方法可以接收一个或两个参数返回元素的开始索引和结束索引。如果只有一个参数则 slice()会返回该索引到数组末尾的所有元素。如果有两个参数则 slice()返回从开始索引到结束索引对应的所有元素其中不包含结束索引对应的元素。如果 slice()的参数有负值那么就以数值长度加上这个负值的结果确定位置。不会改变原数组 let colors [red, green, blue, yellow, purple];
let colors2 colors.slice(1);
let colors3 colors.slice(1, 4);
consle.log(colors2); // [green,blue,yellow,purple]
consle.log(colors3); // [green,blue,yellow]
console.log(colors) //[red, green, blue, yellow, purple]; splice() 删除。需要给 splice()传 2 个参数要删除的第一个元素的位置和要删除的元素数量。 插入。需要给 splice()传 3 个参数开始位置、0要删除的元素数量和要插入的元素。 替换。要传入 3 个参数开始位置、要删除元素的数量和要插入的任意多个元素。 let colors [red, green, blue]; //删除
let removed colors.splice(0,1); // 删除第一项
alert(colors); // green,blue
alert(removed); // red只有一个元素的数组//插入
removed colors.splice(1, 0, yellow, orange); // 在位置 1 插入两个元素
alert(colors); // green,yellow,orange,blue
alert(removed); // 空数组//替换
removed colors.splice(1, 1, red, purple); // 插入两个值删除一个元素
alert(colors); // green,red,purple,orange,blue
alert(removed); // yellow只有一个元素的数组 搜索和位置方法 严格相等 indexOf()、lastIndexOf()和 includes()。这三个方法都可以用来判断当前数组是否存在某个值indexOf()和 lastIndexOf()都返回要查找的元素在数组中的位置如果没找到则返回-1。 includes()返回布尔值includes是es7新增属性 这些方法都接收两个参数要查找的元素和一个可选的起始搜索位置。 let numbers [1, 2, 3, 4, 5, 4, 3, 2, 1];
alert(numbers.indexOf(4)); // 3
alert(numbers.indexOf(4, 4)); // 5 alert(numbers.lastIndexOf(4)); // 5
alert(numbers.lastIndexOf(4, 4)); // 3 alert(numbers.includes(4)); // true
alert(numbers.includes(4, 7)); // false //对象数组let person { name: Nicholas };
let people [{ name: Nicholas }];
let morePeople [person]; alert(people.indexOf(person)); // -1
alert(morePeople.indexOf(person)); // 0
alert(people.includes(person)); // false
alert(morePeople.includes(person)); // true 断言函数 断言函数接收 3 个参数元素、索引和数组本身。其中元素是数组中当前搜索的元素索引是当前元素的索引而数组就是正在搜索的数组。断言函数返回真值表示是否匹配。 找到匹配项后这两个方法都不再继续搜索 find()和 findIndex()方法使用了断言函数。这两个方法都从数组的最小索引开始 find 返回第一个匹配的元素 const people [{ name: Matt, age: 27 }, { name: Nicholas, age: 29 } ,{ name: Jerry, age: 26}
];
console.log(people.find((element, index, array) element.age 28));
// {name: Matt, age: 27} findIndex() 返回第一个匹配元素的索引。 const people [{ name: Matt, age: 27 }, { name: Nicholas, age: 29 } ,{ name: Jerry, age: 26}
];
console.log(people.findIndex((element, index, array) element.age 28));
//0 迭代方法 ECMAScript 为数组定义了 5 个迭代方法每个方法接收两个参数这些方法都不改变调用它们的数组。every() 对数组每一项都运行传入的函数如果对每一项函数都返回 true则这个方法返回 true。 let numbers [1, 2, 3, 4, 5, 4, 3, 2, 1];
let everyResult numbers.every((item, index, array) item 2);
console.log(everyResult); // false some() 对数组每一项都运行传入的函数如果有一项函数返回 true则这个方法返回 true。 let numbers [1, 2, 3, 4, 5, 4, 3, 2, 1];
let everyResult numbers.some((item, index, array) item 2);
console.log(everyResult); // true filter() 对数组每一项都运行传入的函数函数返回 true 的项会组成数组之后返回。 let numbers [1, 2, 3, 4, 5, 4, 3, 2, 1];
let filterResult numbers.filter((item, index, array) item 2);
console.log(filterResult); //[ 3,4,5,4,3] forEach() 对数组每一项都运行传入的函数没有返回值。 本质上forEach()方法相当于使用 for 循环遍历数组。 let newarr []
let numbers [1, 2, 3, 4, 5, 4, 3, 2, 1];
numbers.forEach((item, index, array) { // 执行某些操作
item 1newarr.push(item)
});
console.log(numbers) //[1,2,3,4,5,4,3,2,1]
console.log(newarr) // [2,3,4,5,6,5,4,3,2] map() 对数组每一项都运行传入的函数返回由每次函数调用的结果构成的数组。 let numbers [1, 2, 3, 4, 5, 4, 3, 2, 1];
let mapResult numbers.map((item, index, array) item * 2);console.log(mapResult) // [2, 4, 6, 8, 10, 8, 6, 4, 2];
console.log(numbers) // [1, 2, 3, 4, 5, 4, 3, 2, 1]; 归并方法 ECMAScript 为数组提供了两个归并方法reduce()和 reduceRight()。 reduce() let values [1, 2, 3, 4, 5];
let sum values.reduce((prev, cur, index, array) prev cur);
console.log(sum); // 15