const arr =[{id:1,name:'ljc',age:24},{id:2,name:'xlt',age:23},{id:3,name:'lhw',age:25}]const data = arr.findIndex(item=> item.id ===2)
console.log(data)// 1
(6)includes()方法
表示某个数组是否包含给定的值,返回布尔值。
const arr =[1,2,3]const data = arr.includes(2)
console.log(data)// true
2、String的扩展方法
(1)模板字符串
使用反引号定义
可以解析变量${name}
可以换行
const name ='ljc'const say =`
hello!
my name is ${name}`
console.log(say)
可以调用函数
constfn=()=>{return'world'}const s =`hello ${fn()}`
console.log(s)// hello world
(2)startsWith()和endsWith()
startsWith():表示参数字符串是否在原字符串的开头,返回布尔值
endsWith():表示参数字符串是否在原字符串的尾部,返回布尔值
let s ='hello world!'
console.log( s.startsWith('hello'))// true
console.log( s.startsWith('wo'))// false
console.log( s.endsWith('!'))// true
console.log( s.endsWith('d'))// false
(3)repeat()
repeat(n)方法表示将原字符串重复n次,返回一个新字符串
const s ='hello world\n'const newS = s.repeat(3)
console.log(newS)
六、Set 数据结构
ES6 提供了新的数据结构 Set。它类似于数组,但是成员的值都是唯一的,没有重复的值。
Set函数可以接受一个数组作为参数,用来初始化
const set =newSet()
Set函数可以接受一个数组作为参数,用来初始化。
const set =newSet([1,1,1,2,2,2,3,3,3,4,5,6])
console.log(set)
Set数据结构的实例方法
add(value):添加某个值,返回 Set 结构本身
delete(value):删除某个值,返回一个布尔值,表示删除是否成功
has(value):返回一个布尔值,表示该值是否为Set的成员
clear():清除所有成员,没有返回值
const set =newSet()
set
.add(1).add(2).add(3)
console.log(set)const d = set.delete(1)const h = set.has(1)
console.log(d)
console.log(h)
console.log(set)
set.clear()
console.log(set)
set遍历(foreach())
Set 结构的实例与数组一样,也拥有forEach方法,用于对每个成员执行某种操作,没有返回值。
const set =newSet()
set
.add(1).add(2).add(3)
set.forEach(item=>{
console.log(item)})