ES6实例题
文章目录
一、实例题
const list = [
{ id: 3, name: '张三丰' },
{ id: 5, name: '张无忌' },
{ id: 13, name: '杨逍' },
{ id: 33, name: '殷天正' },
{ id: 12, name: '赵敏' },
{ id: 97, name: '周芷若' },
]
// 运用filter方法进行数组的遍历,filter返回一个新数组,返回的数组是满足return条件后的元素,
// 返回形式为数组,没有符合条件的返回空数组
// 例 arr.filter(function (item, index) { return item > 3 })
// 而结合了许多ES6中字符串的用法,例 模板字符串`${变量}` repeat()函数,将目标字符串重复N次,返回一个新的字符串,不会影响原字符串
// 例 let str_1=str.repeat()
// includes()函数,判断字符串中是否含有指定的子字符串,返回boolean,true表示有,false表示没有,includes()中的第二个参数选填
// 第二个参数表示开始搜索的位置,任何字符串内都有空字符串
// startsWith()函数:用来判断指定的子字符串是否出现在目标字符串的开头位置,结果返回true和false
// endWith()函数:判断指定的子字符串是否出现在目标字符串的尾部
// string.raw函数:作用就是不让换行符(\n)发挥作用,例:console.log(string.raw'hello\n world')
console.log(list.filter(function (item) { return item.name.startsWith('杨') }))
console.log(list.filter(function (item) { return item.name.includes('天') }))
console.log(list.find(function (item) { return item.name == '周芷若' }).id);
二、
2.读入数据
代码如下(示例):
const list = [
{ id: 3, name: '张三丰' },
{ id: 5, name: '张无忌' },
{ id: 13, name: '杨逍' },
{ id: 33, name: '殷天正' },
{ id: 12, name: '赵敏' },
{ id: 97, name: '周芷若' },
]
// for (let [key, value] of (list.entries()))
// map方法遍历数组,map函数中有三个参数,例:arr.map(function (item, index) { return item }) map有返回值,返回一个新数组
console.log(list.map(function (item, id) {
var time = new Date()//创建日期对象
console.log(time.toLocaleString());
return item.id * 2
// updateTime = getddate()
// var time = new Date()
// updateTime = getddate()
// console.log(time.toLocaleString());
}));