PHP前端开发

JavaScript - 解构数组和对象 [实时文档]

百变鹏仔 3天前 #JavaScript
文章标签 数组
const nums = [8,4,5];const num1 = nums[0];const num2 = nums[1];const num3 = nums[2];console.log(num1, num2, num3);is reduced to const [x,y,z] = nums;console.log(x, y, z);three const variables named x,y,z are created in this step
const girl = {  name: 'melania',  friends: ['alina', 'alice', 'ayesha', 'anamika', 'anaya'],  eats: ['roasted', 'pizza', 'burger', 'rice', 'manchurian'],};const [first, second] = girl.friends;console.log(first, second);const [,,,fourth,last] = girl.eats;console.log(fourth, last);

交换变量[变异]

let array = [5,6];let [a,b] = array;console.log(`a: ${a}, b:${b}`);[b,a] = [a,b];console.log(`a: ${a}, b:${b}`);

函数返回一个数组,立即破坏结果,这允许我们从函数返回多个值

const girl = {  name: 'melania',  friends: ['alina', 'alice', 'ayesha', 'anamika', 'anaya'],  eats: ['roasted', 'pizza', 'burger', 'rice', 'manchurian'],  drinks: ['juice','coffee','coke'],  order: function(eat,drink){    return [this.eats[eat],this.drinks[drink]];  }};const [maincourse, drinks] = girl.order(2, 2);console.log(`maincourse: ${maincourse}`);console.log(`drinks: ${drinks}`);

解构嵌套数组

let nums = [5,3,[8,7,9,3]];let [x,y,z] = nums;console.log(`x: ${x}`); // 5console.log(`y: ${y}`); // 3console.log(`z: ${z}`); // 8,7,9,3let nums2 = [5,3,[8,7]];let [x,,[y,z]] = nums2;console.log(`x: ${x}`, `y: ${y}`, `z: ${z}`); // 5 8 7 

从未知大小的数组解构:

const names = ['michael','charlie','peter'];let [w='xxx',x='xxx',y='xxx',z='xxx'] = names;console.log(w,x,y,z); // 'michael' 'charlie' 'peter' 'xxx'

解构对象:

const girl = {  name: 'melania',  friends: ['alina', 'alice', 'ayesha', 'anamika', 'anaya'],  eats: ['roasted', 'pizza', 'burger', 'rice', 'manchurian'],  drinks: ['juice','coffee','coke'],  works: {        mtwt: {          start: 9,          end: 5        },        fri: {          start:9,          end: 3        }  }};const {name, works, drinks} = girl;console.log(name);console.log(works);console.log(drinks);// replace long property names with custom names:const {name:user, works:timings, drinks:enjoys} = girl;console.log(user);console.log(timings);console.log(enjoys);//destructuring data from api calls returned in the form of objects i.e attaching a default value to a property that does not exist on object received from an api call// details does not exist, so default value is assignedconst { details = [], eats: loves = [] } = girl;console.log(details);// eats exist but is renamed as loves, hence default value won't applyconsole.log(loves);
## mutating variables using object destructuringlet x = 10;let y = 20;let obj = {x:1, y:2, z:3};{x,y} = obj; // errorwhen we start a line with a '{', then js expects a code-block. and we cannot assign anything to a code-block on lhs using = operator. hence, an error is thrown. the error is resolved by wrapping into () as shown below({x,y} = obj); //{ x: 1, y: 2, z: 3 }

解构嵌套对象

const girl = {  name: 'Melania',  friends: ['Alina', 'Alice', 'Ayesha', 'Anamika', 'Anaya'],  eats: ['Roasted', 'Pizza', 'Burger', 'Rice', 'Manchurian'],  drinks: ['Juice','Coffee','Coke'],  works: {        mtwt: {          start: 9,          end: 5        },        fri: {          start:10,          end: 2        }  }};let { fri } = works;console.log(fri);// Destructuring the fri object using the same property names start, endlet {fri: {start, end}} = works;console.log(start, end);// Further renaming for shortening start as 'b' and end as 'e'let {fri: {start: b, end: e}} = works;console.log(b, e);const girl = {  name: 'Melania',  friends: ['Alina', 'Alice', 'Ayesha', 'Anamika', 'Anaya'],  eats: ['Roasted', 'Pizza', 'Burger', 'Rice', 'Manchurian'],  drinks: ['Juice','Coffee','Coke'],  works: {        mtwt: {          start: 9,          end: 5        },        fri: {          start:10,          end: 2        }  },  // these destructured property-names have to be same as they are passed inside the girl.sleep(). Order need not be same.  sleep: function ({time='NA', address='NA', color = 'NA', duration='NA'}){    console.log(`${this.name} sleeps at ${address} for ${duration} in ${color}light for ${duration}. She loves to eat ${this.eats[0]}`);  }};// A single object is passed, which will be destructured by the method inside the object extracting all values via destructuringgirl.sleep({time: '10pm', address:'home', color: 'blue', duration: '7hrs'});girl.sleep({time: '9pm', duration: '7hrs'});