Object數據操作 - daniel-qa/Vue GitHub Wiki
- 複製 Object
// 複製 Object
this.sharedSyllabusMapping = res ? { ...res } : {};
// 複製陣列
this.sharedSyllabusMapping = Array.isArray(res) ? [...res] : [];
- 判斷有沒有回傳值
if (res.data && Object.keys(res.data).length > 0) {
// 有資料
} else {
// 沒資料
}
res.data:確保不是 null 或 undefined
Object.keys(res.data).length:物件的屬性數量,如果是 0 就代表沒有資料
- 新增一筆值
aprule.irs = "true";
如果 irs 屬性不存在,則會新增該屬性並賦值。
如果 irs 屬性已經存在,則會更新該屬性的值為 "true"。
-
Object.keys(obj) : 會把物件裡的「屬性名稱」抓出來變成陣列。
order.serial?.forEach(s => {
s.authKey = Object.keys(s.aprule || {})
})
- Object Destructuring(物件解構)
由 ojbect 直接抓出想要的資料
Object Destructuring(物件解構)
const user = { name: "Alex", age: 30, role: "admin" }
// 解構賦值
const { name, age } = user
// 相當於:
const name = user.name
const age = user.age