对象和数组的解构以及数组的遍历4种写法

1.对象的函数解构

let json={
    a:"websong",
    b:"web"
};
function fnu({a,b}) {//非常有用,这里对应的是传入对象的参数,操作对象很便捷
    console.log(a,b)
}
fnu(json);

2.数组的解构

let arr=['websong','js','前端'];
function res(a,b,c) {
    console.log(a,b,c)
}
res(...arr);//数组解构,扩展写入

3.in 判断

let obj={
    a:"websong",
    b:"123456"
};
console.log('a' in obj);//判断对象里有没有这个属性
let arrs=[,,,];
console.log(0 in arrs);//判断数组的空位
console.log(arrs.length>0)//true,这种是不严谨的,当然,也没有病人会写上面那种数组,但in的判断更加精准

4.数组的遍历

let arr_=['websong','23岁','web'];
arr_.forEach((val,index)=>console.log(index,val))
arr_.filter(x=>console.log(x))
arr_.some(x=>console.log(x));
for (let i in arr_){console.log(arr[i])}
for (let i of arr_){console.log(i)}
当然还有一个for原始循环,就不写了

5.将数组转成字符串,一般用在ajax传值上,比如拼接

console.log(arr.toString());//将数组转成字符串
console.log(arr.join('|'));//将数组字符串,替换成|

  

原文地址:https://www.cnblogs.com/webSong/p/7384943.html