Xiaobian do not know how everyone’s May Day, anyway, for xiaobian, there are still some harvest, today xiaobian continue to update SOME new features of JS objects, looking forward to progress with you. You can also follow my wechat public number, snail full stack.
Object.values() : Get an array of values in an Object
const obj = {
name:"lilei".web:"www.baidu.com".course:"math"
}
console.log(Object.values(obj)) // ["lilei","www.baidu.com","math"]
Copy the code
Object.entries() : returns a two-digit array, each containing a key and a value
const obj = {
name:"lilei".web:"www.baidu.com".course:"math"
}
console.log(Object.entries(obj)) // [["name","lilei"],["web","www.baidu.com"],["course","math"]]
console.log(Object.entries(["a"."b"."c"])) // [["0","a"],["1","b"],["2","c"]]
Copy the code
Loop traversal: Combined with deconstruction and string templates in ES6
const obj = {
name:"lilei".web:"www.baidu.com".course:"math"
}
for(let [key,val] of Object.entries(obj)){
console.log(`${key}:${val}`) // name:lilei web:www.baidu.com course:math
}
Copy the code
4. Grammar before ES8
const obj = {
name:"lilei".web:"www.baidu.com".course:"math"
}
console.log(Object.keys(obj)) // ["name","web","course"]
const res = Object.keys(obj).map(key= > obj[key])
console.log(res) // ["lilei","www.baidu.com","math"]
Copy the code
Five, the Object. GetOwnPropertyDescriptors () : access to the Object itself attribute modifiers
const obj = {
name:"lilei".course:"math"
}
const desc = Object.getOwnPropertyDescriptors(obj)
console.log(desc)
/ / {
// name:{
// value:"lilei", // Default value
// writable:true, // whether the attribute can be changed
Enumerable :true; // Enumerable :true; In loop traversal
// Signals :true // Can be freely deleted by delete
/ /},
// course:{
// value:"math",
// writable:true,
// enumerable:true,
// configurable:true
/ /}
// }
Copy the code
6. Combine with Reflect
const obj= {}
Reflect.defineProperty(obj,'name', {value:"lilei".writable:false.configurable:false.enumerable:true
})
Reflect.defineProperty(obj,'age', {value:18.writable:false.configurable:false.enumerable:false
})
console.log(obj) // {name:"lilei"}
obj.name= "zhangsan"
console.log(obj) // {name:"lilei"}
delete obj.name
console.log(obj) // {name:"lilei"}
for(let key in obj){
console.log(key) // Only name. There is no age
}
Copy the code
Seven, Object. GetOwnPropertyDescriptor ()
const obj = {
name:"lilei".course:"math"
}
const desc = Object.getOwnPropertyDescriptor(obj,"name")
console.log(desc)
/ / {
// value:"lilei", // Default value
// writable:true, // whether the attribute can be changed
Enumerable :true; // Enumerable :true; In loop traversal
// Signals :true // Can be freely deleted by delete
// }
Copy the code