<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<script type="text/javascript">
//利用解构赋值过滤对象中部分属性,两种方法
let student={
age: 18,
name: 'kysue',
sex: "男"
}
/*方法一 过滤掉name属性 */
let {sex,...result}=student;
console.log('result',result);//{age: 18, name: "kysue"}
/*方法二 过滤掉age属性*/
const fun=({name,sex})=>({name,sex});
let param=fun(student);
console.log('param',param);//{name: "kysue", sex: "男"}
/* 要求:
现有数组对象:
featureTags: [{featureId: 1,featureContent: '全新未用',checked: '0'},
{featureId: 2,featureContent: '验货面付',checked: '0'},
{featureId: 3,featureContent: '快递包邮',checked: '0'},
{featureId: 4,featureContent: '保修期内',checked: '0'},
{featureId: 5,featureContent: '专柜正品',checked: '0'},
{featureId: 6,featureContent: '有发票',checked: '0'}]
要求使用ES6语法将数组内每个对象的checked属性去掉。 */
let featureTags=[
{featureId: 1,featureContent: '全新未用',checked: '0'},
{featureId: 2,featureContent: '验货面付',checked: '0'},
{featureId: 3,featureContent: '快递包邮',checked: '0'},
{featureId: 4,featureContent: '保修期内',checked: '0'},
{featureId: 5,featureContent: '专柜正品',checked: '0'},
{featureId: 6,featureContent: '有发票',checked: '0'},
];
//test
let arrTest=[];
for (let s of featureTags) {
let {checked,...res}={...s}
arrTest.push(res);
}
console.log('arrTest:',arrTest);
//1.使用push,将修改后的数组放入新数组中
let newArr=[];
for(let i of featureTags){
// console.log(i);
const f=({featureId,featureContent})=>({featureId,featureContent})
;
let param=f(i);
newArr.push(param);
}
console.log(newArr);
//2.Array.map()方法 通过指定函数处理数组的每个元素,并返回处理后的数组。
var arr = featureTags.map(({featureId,featureContent})=>{
return {featureId,featureContent}
})
console.log(arr)
//3.Array.map()更牛逼的方法
let bestArr=featureTags.map(v=>{
let {checked,...reset}={...v}
return reset;
})
console.log('bestArr:',bestArr);
//3.使用foreach实现一下
let bestArr2=featureTags.forEach(item=>{
let {checked,...reset}={...item}
return reset;
})
console.log('bestArr2:',bestArr2);
//4.向对象数组中添加新的属性
var arr1 = featureTags.map(v=>{
return {...v,test:'hello'}
})
console.log(arr1);
//5.连接两个数组的es6方式(...将数组序列化,成为逗号隔开的序列)
arr3 = [1,2];
arr4= [3,4,5];
arr3.push(...arr4);
console.log(arr3);
</script>
</body>
</html>
解构赋值的应用
最新推荐文章于 2023-04-05 22:49:33 发布