当前位置:网站首页>ES6 three-dot operator, array method, string extension method
ES6 three-dot operator, array method, string extension method
2022-08-02 04:00:00 【yorup】
一、Using the three-dot operator
1:函数传不定参数,验证数组的长度.
function demo(a,...b){
console.log(a,b);//b为数组 2,3,4,5
}
demo(1,2,3,4,5);
2:与解构使用
let [a,...b] = [1,2,3,4,5];
console.log(a,b);
3:与数组解构使用 函数传对象
function demo({username,password}){
console.log(username,password);
}
demo({username:'root',password:'123456'});
二、扩展运算符(...)
1:用...输出数组
const arr = [1,2,3];
console.log(...arr);
2:合并数组
const arr1 = [1,2,3];
const arr2 = [4,5,6];
const arr3 = [...arr1,...arr2];
console.log(arr3);
3:将类数组转为真正的数组
<body>
<div>1</div>
<div>2</div>
<div>3</div>
</body>
<script>
const divEle = document.getElementsByTagName("div");
const arr = [...divEle];
console.log(arr);
let str = "1234";
console.log([...str]);
</script>
练习:创建一个函数:用扩展运算符计算两个数的和.
function demo(a,b){
return a+b;
}
const arr1 = [1,2];
const arr2 = [4,5];
console.log(demo(...arr1));
console.log(demo(...arr2));
三、Array对象的方法
1:Array.from() 将伪数组或可遍历对象转换为真正的数组
let str = "1234";
const arr = Array.from(str);
console.log(arr);
in the method below,对象里面的keycorresponds to the subscript of the array,不能是其他值,length表示数组的长度
const Arr = {
2:"a",
3:"b",
length:4,
}
console.log(Array.from(Arr));
2:array.find返回数组符合条件第一个元素的值
const arr = [1,2,3,4];
let num = arr.find(item=>item==3);
console.log(num);
找数组包对象
const arr = [
{realname:"张三1",age:18},
{realname:"张三2",age:17},
{realname:"张三3",age:19},
{realname:"张三4",age:17},
];
console.log(arr.find(item=>item.age==17));
3:array.findindex找到符合条件的第一个元素的索引
const arr = [1,2,3,4];
console.log(arr.findIndex(item=>item==4));
const arrobj = [
{realname:"张三1",age:18},
{realname:"张三2",age:19},
{realname:"张三3",age:15},
{realname:"张三4",age:14},
]
console.log(arrobj.findIndex(item=>item.age==19));
4:array.includes():找出某个数组是否包含给定的值.
const arr = [1,2,3,4];
console.log(arr.includes(10));//有就返回true 没有就返回false
四、字符串扩展方法
1:模板字符串的用法 ${}
function demo(){
return "end";
}
let es6 = "es6!";
let str = `hello,${es6},${demo()}`;
console.log(str);
2:startsWith和endsWith用法
let str = "hello,es6!";
console.log(str.startsWith("hello"));//判断某个字符串前面是否包含hello 有就为true
console.log(str.endsWith("es6!"));//与startsWith相反
3:repeat字符串重复次数
console.log("hello".repeat(4));
练习:
1:找到一组同学中考试分数及格的第一个同学并输出到页面上.
<body>
<ul id="name">
<li>张三,50</li>
</ul>
<hr>
<h1>The first student to pass the grade is</h1>
<p id="realname"></p>
<script>
var scores = [
{ name: '小李', score: 50 },
{ name: '小明', score: 40 },
{ name: '小红', score: 70 },
{ name: '小王', score: 90 },
]
let str = '';
for (let i = 0; i < scores.length; i++) {
str = str + `<li>姓名:${scores[i].name},分数:${scores[i].score}</li>`;
}
document.getElementById('name').innerHTML = str;
let findscore = scores.find(item => item.score >= 60);
// console.log(findscore);
document.getElementById('realname').innerText = `姓名:${findscore.name},分数:${findscore.score}`;
</script>
</body>
2:找出大于指定年龄(页面input框接收)的第一个人,并输出这个人的位置
<body>
<ul id="persons"></ul>
<input type="text" placeholder="请输入年龄" id="findage">
<input type="button" id="findbtn" value="查找">
<hr>
<h1 id="res"></h1>
<script>
let findage = document.getElementById('findage');
let findbtn = document.getElementById('findbtn');
let res = document.querySelector('#res');
var personsarr = [
{ name: '小李', age: 35 },
{ name: '小明', age: 23 },
{ name: '小红', age: 45 },
{ name: '小王', age: 12 },
];
let str1 = '';
let num;
for (let i = 0; i < personsarr.length; i++) {
str1 = str1+`<li>姓名:${personsarr[i].name},年龄:${personsarr[i].age}</li>`;
}
document.getElementById('persons').innerHTML = str1;
findbtn.addEventListener('click',()=>{
num = personsarr.findIndex(item=>item.age==findage.value);
console.log(num);
if (num==-1) {
res.innerText = '查无此人';
} else {
res.innerText = `与${findage.value}People of the same age are in the first${num+1}个位置`
}
})
</script>
</body>
有疑问可评论
边栏推荐
- 一个网络安全小白鼠的学习之路——nmap的基本使用
- easyswoole uses redis to perform geoRadiusByMember Count invalid fix
- Phonebook
- 16.JS事件, 字符串和运算符
- CTF入门之md5
- [symfony/finder]最好用的文件操作库
- 阿里云MySQL5.7安装以及部分主要问题(总和)
- hackmyvm: may walkthrough
- DVWA drone installation tutorial
- ES6 array extension methods map, filter, reduce, fill and array traversal for…in for…of arr.forEach
猜你喜欢
Pycharm打包项目为exe文件
ES6 array extension methods map, filter, reduce, fill and array traversal for…in for…of arr.forEach
(3)Thinkphp6数据库
Introduction to PHP (self-study notes)
hackmyvm: juggling walkthrough
What are the killer super powerful frameworks or libraries or applications for PHP?
(1) introduction to Thinkphp6, installation view, template rendering, variable assignment
Praying: 1 vulnhub walkthrough
IP access control: teach you how to implement an IP firewall with PHP
PHP反序列化漏洞
随机推荐
PHP有哪些杀手级超厉害框架或库或应用?
[symfony/mailer] An elegant and easy-to-use mail library
Introduction to PHP (self-study notes)
(1) introduction to Thinkphp6, installation view, template rendering, variable assignment
JS objects, functions and scopes
hackmyvm: juggling walkthrough
Advanced Operations on Arrays
4.PHP数组与数组排序
kali安装IDEA
easyswoole 使用redis执行geoRadiusByMember Count无效修复
1. Beginning with PHP
IO stream, encoding table, character stream, character buffer stream
一次代码审计的笔记(CVE-2018-12613 phpmyadmin文件包含漏洞)
hackmyvm: kitty walkthrough
17. JS conditional statements and loops, and data type conversion
MOMENTUM: 2 vulnhub walkthrough
(2) 顺序结构、对象的布尔值、选择结构、循环结构、列表、字典、元组、集合
(2) Thinkphp6 template engine ** tag
查询数据库中所有表的索引,并且解析成sql
2. PHP variables, output, EOF, conditional statements