目录
4.startsWith()、endsWith()的基本使用方法
2:找出大于指定年龄(页面input框接收)的第一个人,并输出这个人的位置
剩余参数语法允许我们将一个不定数量的参数表示为一个数组,不定参数定义方式,这种方式很方便的去声明不知道参数情况下的一个函数。
- function demo(a,...b){
- console.log(a,b);//b为数组 2,3,4,5
- }
- demo(1,2,3,4,5);
- let [a,...b] = [1,2,3,4,5];
- console.log(a,b);//1 [2,3,4,5]
- function demo({username,password}){
- console.log(username,password);//root 123456
- }
- demo({username:'root',password:'123456'});
... 扩展运算符可以将数组或者对象转为用逗号分隔的参数序列。
- const arr = [1,2,3];
- console.log(...arr); //1,2,3,相当于下面的代码
- console.log(1,2,3);
- const arr1 = [1,2,3];
- const arr2 = [4,5,6];
- const arr3 = [...arr1,...arr2];
- console.log(arr3);// [1,2,3,4,5,6]
- html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>title>
- head>
- <body>
-
- <div>1div>
- <div>2div>
- <div>3div>
-
- <script>
-
- const divEle = document.getElementsByTagName("div");
- const arr = [...divEle];
- console.log(arr);
-
- let str = "1234";
- console.log([...str]);
-
- script>
-
- body>
- html>
- const arr1 = [1,2,3];
- const arr2 = [...arr1];
- arr2[0]=5;
- console.log('这是arr1:'+ arr1+'\n'+'这是arr2:'+arr2);
- function demo(a,b){
- return a+b;
- }
- const arr1 = [1,2];
- const arr2 = [4,5];
-
- console.log(demo(...arr1));
- console.log(demo(...arr2));
将伪数组或可遍历对象转换为真正的数组。
- Array.from('12345') // [1,2,3,4,5]
-
- let arr1 = {
- 1:'a',
- 2:'b',
- 'length':3
- }
- console.log(Array.from(arr1));//undefined ,a,b.
find() 方法返回通过测试(函数内判断)的数组的第一个元素的值。
- let arr1 = [1,2,3,2];
- let target = arr1.find( item => item==2);
- console.log(target);//2,如果未找到,返回undefined
-
-
-
-
-
- let person = [
-
- {name:"张三",age:16},
- {name:"李四",age:17},
- {name:"王五",age:18},
- ]
-
- let target = person.find((item,index)=>{return item.name=='张三'});
- console.log(target.name);//张三
找到符合条件的第一个元素的索引
- let ary = [1, 5, 10, 15];
- let index = ary.findIndex((item, index) => item > 9);
- console.log(index); // 2
找出某个数组是否包含给定的值。有就返回true 没有就返回false
- const arr = [1,2,3,4];
- console.log(arr.includes(10));//false
使用反引号定义 let name = `张三`;
- let name = '张三';
- let sayHello = `hello,my name is ${name}`; // hello, my name is Lee
- let result = {
- name: '张三',
- age: 28,
- sex: '男'
- }
- let html =
- `
-
- ${result.name}
- ${result.age}
- ${result.sex}
-
- `
- console.log(html);
- const sayName = function () {
- return '张三';
- };
- let greet = `${sayName('张三')} ,你好!`;
- console.log(greet); //张三 ,你好!
- startsWith():表示参数字符串是否在原字符串的头部,返回布尔值
- endsWith():表示参数字符串是否在原字符串的尾部,返回布尔值
- let str = "hello,es6!";
- console.log(str.startsWith("hello"));
- //判断某个字符串前面是否包含hello 有就为true
- console.log(str.endsWith("es6!"));//与startsWith相反
repeat方法表示将原字符串重复n次,返回一个新字符串。
console.log('hello'.repeat(2));//hellohello
- html>
- <html lang="en">
-
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
- head>
-
- <body>
-
- <ul id="scores">ul>
-
- <hr>
-
- <h1>第一个及格的学生是:h1>
- <p id="findname">p>
- <br>
-
- <script>
- const scores = [
- { realname: '张三', score: 23 },
- { realname: '李四', score: 36 },
- { realname: '王五', score: 63 },
- { realname: '赵六', score: 88 },
- { realname: '小明', score: 66 }
- ]
-
- let str = '';
- let findName = '';
-
- for (let i = 0; i < scores.length; i++) {
- str = str + `
- 姓名:${scores[i].realname},分数:${scores[i].score}
` - document.getElementById('scores').innerHTML = str
- }
- findName = scores.find(item => item.score >= 60);
- document.getElementById('findname').innerHTML = `姓名:${findName.realname}
分数:${findName.score}` -
- script>
-
- body>
-
- html>
- html>
- <html lang="en">
-
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
- head>
-
- <body>
-
- <ul id="person">ul>
- <hr>
- <input placeholder="输入要找的人的年龄" type="text" id="findage" value="" />
- <input type="button" value="查找" id="btn" />
-
- <h1>h1>
- <script>
- const persons = [
- { realname: "小高", age: 16 },
- { realname: "小明", age: 15 },
- { realname: "小红", age: 14 },
- { realname: "小丽", age: 19 },
- ];
-
- let str = '';
- let btn = document.getElementById('btn');
- let findage;//接收查找的值
-
- for (var i = 0; i < persons.length; i++) {
- str = str + `
- 姓名:${persons[i].realname},年龄:${persons[i].age}
` -
- }
- document.querySelector('#person').innerHTML = str;
- btn.addEventListener('click', () => {
- let num;
- findage = document.querySelector('#findage').value;
-
- num = persons.findIndex(item => item.age == findage);
- // console.log(num)
- if (num == -1) {
- document.querySelector('h1').innerHTML = `查无此人`;
-
- } else {
- num = num + 1;
- document.querySelector('h1').innerHTML = `与${findage}年龄相等的位置是:${num}`
- }
- })
-
-
- script>
- body>
-
- html>