2.2.3 上机训练
上机练习2 一一 对象的解构赋值使用
需求说明
假设有一段 JSON 对象数据,利用对象的解构赋值来快速提取 JSON 数据的值。
前端开发与后台交互得到的数据格式,一般情况下为 JSON 对象数据格式,利用对象的解构赋值可以很方便地提取 JSON 对象中的数据。
var hzhJson = {
id: 1,
name: "黄子涵",
status: 'OK',
data: [123, 542],
brother: "黄春钦",
mother: "陈兰英",
tel: 19124896017
};
var {
id: id,
name: name,
status: status,
data: data,
brother: brother,
mother: mother,
tel: tel
} = hzhJson;
console.log("输出id:");
console.log("id = " + id);
console.log("");
console.log("输出name:");
console.log("name = " + name);
console.log("");
console.log("输出status:");
console.log("status = " + status);
console.log("");
console.log("输出data:");
console.log("data = " + data);
console.log("");
console.log("输出brother:");
console.log("brother = " + brother);
console.log("");
console.log("输出mother:");
console.log("mother = " + mother);
console.log("");
console.log("输出tel:");
console.log("tel = " + tel);
[Running] node "e:\HMV\JavaScript\JavaScript.js"
输出id:
id = 1
输出name:
name = 黄子涵
输出status:
status = OK
输出data:
data = 123,542
输出brother:
brother = 黄春钦
输出mother:
mother = 陈兰英
输出tel:
tel = 19124896017
[Done] exited with code=0 in 0.186 seconds