let arr = [1,2,3,4]
// ...arr 1,2,3
console.log(...arr) // 1 2 3
let arr1 = [1,2,3]
let arr2 = [3,4,5]
//第一种方法
let arr3 = [...arr1,...arr2]
console.log(arr3)
// 第二种方法
arr1.push(...arr2)
console.log(arr1)
let oDivs = document.getElementsByTagName('div')
console.log(oDivs) //伪数组
console.log([...oDivs]) //真实的数组
let obj = {
'0':'张山',
'1':'李四',
'2':'王五',
'length':3
}
let arr = Array.from(obj)
console.log(arr) //输出了一个真正的数组
let obj1 = {
'0':'1',
'1':'2',
'2':'3',
'length':3
}
let arr1 = Array.from(obj1,(item)=>{
return item*2
})
console.log(arr1) //输出的 是 [2,4,6]
let arr = [
{id:1,name:'张三'},
{id:2,name:'李四'},
]
let target = arr.find((item,index)=>{
return item.id === 2
})
console.log(target)
{id: 2, name: "李四"}
let arr = [1,5,10,15]
let index = arr.findIndex((value,index)=>value>9);
console.log(index)
let beeleans1 = [1,2,3].includes(2);
let beeleans2 = [1,2,3].includes(4);
console.log(beeleans1)
console.log(beeleans2)
let name = `张山`
console.log(name)
let sayHello = `Hello,my name is ${name}`;
console.log(sayHello)
let result = {
name:'suni1024',
age:20,
sex:'男'
}
let html = `<div>
<span>${result.name}</span>
<span>${result.age}</span>
<span>${result.sex}</span>
</div>`;
console.log(html)
const sayHello = ()=>{
return `卧槽无情`
}
let greet = `${sayHello()},二货`
console.log(greet)
let str = `Hellow world!`;
const a = str.startsWith('Hello');
const b = str.endsWith('!!');
console.log(a) //true
console.log(b) //false
let x = 'X'.repeat(3)
let Hello = 'Hello'.repeat(2)
console.log(x) //xxx
console.log(Hello) //HelloHello
ES6提供了新的数据结构Set.类似于数组,但是成员的值都是唯一的,没有重复的值
const s = new Set();
Set 函数可以接受一个数组作为参数,用来初始化.
const set = new Set([1,2,3,4,4])
console.log(s)
console.log(s.size)
const set2 = new Set(['a',3,4,4])
console.log([...set2])
const s4 = new Set();
s4.add(1).add(2).add(3);//向set结构中添加值
console.log(s4)
s4.delete(2) //删除set结构中的2值
console.log(s4)
let a = s4.has(1) //表示set 结构中是否有1这个值,返回布尔值
console.log(a)
s4.clear() //清楚set结构中的所有值
console.log(s4)
const mySet = new Set();
mySet.add("蚂蚁部落");
mySet.add(4);
mySet.add("www.softwhy.com");
console.log(mySet)
const s5 = new Set(['a','b','c','d'])
s5.forEach(value=>{
console.log(value)
})
原文:https://www.cnblogs.com/suni1024/p/12197800.html