//includes():返回布尔值,表示是否找到了参数字符串。
//startsWith():返回布尔值,表示参数字符串是否在源字符串的头部。
//endsWith():返回布尔值,表示参数字符串是否在源字符串的尾部。
var s = 'Hello world!';
s.startsWith('Hello') // true
s.endsWith('!') // true
s.includes('o') // true
// 以上这三个方法都支持第二个参数,表示开始搜索的位置。
s.startsWith('world', 6) // true
s.endsWith('Hello', 5) // true 这个是从第五个之前开始查找
s.includes('Hello', 6) // false
// repeat方法返回一个新字符串,表示将原字符串重复n次。
'x'.repeat(3) // "xxx"
'hello'.repeat(2) // "hellohello"
'na'.repeat(0) // ""
// 参数如果是小数,会被取整。
'na'.repeat(2.9) // "nana"
//Array.from方法用于将两类对象转为真正的数组 也可以有第二个参数 Array.from(arr)
let arr=[1,,,2,3,,4];
let a= Array.from(arr,(v)=>v||0)
console.log(arr)//1,0,0,2,3,0,4
//数组实例的find()返回符合条件的第一个字符,当然也可以用来判断添加获取新的数组,如果是对象数组也可以添加多个参数 function(v,x,d)v代表属性 x代表值
var obj=[1,2,3,4,5,6,7]
console.log(obj)
let as=[]
var sobj=obj.find(function(v){
if(v<3){
as.push(v)
}
})
console.log(as)//1,2
//ES6提供三个新的方法——entries(),keys()和values()——用于 遍历数组。可以用for...of循环进行遍历,唯一的区别是keys()是对键名的遍历、values()是对键值的遍历,entries()是对键值对的遍历。
for (let index of ['a', 'b'].keys()) {
console.log(index);
}
// 0
// 1
for (let elem of ['a', 'b'].values()) {
console.log(elem);
}
// 'a'
// 'b'
for (let [index, elem] of ['a', 'b'].entries()) {
console.log(index, elem);
}
// 0 "a"
// 1 "b"
// forEach方法
[,'a'].forEach((x,i) => console.log(i)); // 1
// filter方法
['a',,'b'].filter(x => true) // ['a','b']
// every方法
[,'a'].every(x => x==='a') // true
// some方法
[,'a'].some(x => x !== 'a') // false
// map方法
[,'a'].map(x => 1) // [,1]
// join方法
[,'a',undefined,null].join('#') // "#a##"
// toString方法
[,'a',undefined,null].toString() // ",a,,"