每日一图
目录
...扩展运算符可以将数组或者对象转为用逗号分隔的参数序列。
find() 方法返回通过测试(函数内判断)的数组的第一个元素的值。
定义:用于找出第一个符合条件的数组成员的位置,如果没有找到返回-1。
- startsWith():表示参数字符串是否在原字符串的头部,返回布尔值
- endsWith():表示参数字符串是否在原字符串的尾部,返回布尔值
map() 方法返回一个新数组,数组中的元素为原始数组元素调用函数处理后的值。
filter() 方法创建一个新的数组,新数组中的元素是通过检查指定数组中符合条件的所有元素。
reduce() 方法接收一个函数作为累加器,数组中的每个值(从左到右)开始缩减,最终计算为一个值。
第一节:array的扩展方法、
1 展开语法
...扩展运算符可以将数组或者对象转为用逗号分隔的参数序列。
将 类数组转为数组
let eledivs = document.getElementsByTagName('div');
eledivs = [...eledivs];//Array.prototype.slice.call(eledivs);
2 Array.from()方法
将伪数组或可遍历对象转换为真正的数组。
举例1:
Array.from('12345') // [1,2,3,4,5]
举例2:
let arr1 = {
1:'a',
2:'b',
'length':3
}
console.log(Array.from(arr1));//undefined ,a,b.
3 array.find() 方法
find() 方法返回通过测试(函数内判断)的数组的第一个元素的值。
举例1:
let arr1 = [1,2,3,2];
let target = arr1.find( item => item==2);
console.log(target);//2,如果未找到,返回undefined
举例2:
let person = [
{name:"张三",age:16},
{name:"李四",age:17},
{name:"王五",age:18},
]
let target = person.find((item,index)=>{return item.name=='张三'});
console.log(target.name);
4 array.findindex()方法
定义:用于找出第一个符合条件的数组成员的位置,如果没有找到返回-1。
let ary = [1, 5, 10, 15];
let index = ary.findIndex((item, index) => item > 9);
console.log(index); // 2
5 array.includes()方法
定义:判断某个数组是否包含给定的值,返回布尔值。
let ary = [1, 5, 10, 15];
console.log(ary.includes(5));//true
Array.from
1:字符串形式
let str = "abcde";
console.log(Array.from(str));
2:将对象形式数组 转为数组
const Num = {
0:"0",
1:"a",
2:"b",
length:3,
}
console.log(Array.from(Num));
3:将类数组转为数组
const liNode = document.querySelectorAll("li");
console.log(Array.from(liNode) instanceof Array);
二 array.find 查找符合条件的第一个值
const persons = [5,11,19,20];
const item = persons.find(item=>item>10)
console.log(item);
const persons = [
{realname:"张三",age:15},
{realname:"李四",age:19},
{realname:"王五",age:2