es6解构的使用场景

版权声明:未经本人同意不得私自转载 https://blog.csdn.net/qq_40190624/article/details/82718961
// 使用场景为将数组转化成对象
const point =[
    [4,5],
    [10,1],
    [0,40]
];
/**
 * 期望数据格式 
 * [
 *  {x:4,y:5},
 *  {x:10,y:1},
 *  {x:0,y:40},
 * ]
 */
let newPoints = point.map(([x,y]) =>{
    // 方式一
    // return {x:x,y:y}
    // 方式二
    return {x,y}
})
console.log(newPoints)//两种方式得到的都为 [ { x: 4, y: 5 }, { x: 10, y: 1 }, { x: 0, y: 40 } ]

猜你喜欢

转载自blog.csdn.net/qq_40190624/article/details/82718961