Object.keys(obj)
Object.keys(obj) 返回对象的key组成的数组
返回一个所有元素为字符串的数组,其元素来自于从给定的object上面可直接枚举的属性,不会返回原型上的方法。
1 2 3 4 5 6 7 8
| const obj = { firstname:"John", lastname:"Doe", age:50, eyecolor:"blue" }; console.log(Object.keys(obj)) ['firstname', 'lastname', 'age', 'eyecolor']
|
Object.values(obj)
Object.values(obj) 返回对象的value组成的数组
返回一个由value组成的数组,其元素是在对象上找到的可枚举属性值。
1 2 3 4 5 6 7 8
| const obj = { firstname:"John", lastname:"Doe", age:50, eyecolor:"blue" }; console.log(Object.values(obj)) ['John', 'Doe', 50, 'blue']
|
#Object.assign()
Object.assign() 可以将源对象复制到目标对象中
Object.assign(target, …sources)
target
为目标对象,...sources
为源对象(可以为多个对象)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| const obj = { firstname:"John", lastname:"Doe", age:50, love:{ color: "blue", sport: "football" } }; const copy = Object.assign({}, obj); console.log(copy)
const obj = { firstname:"John", lastname:"Doe", age:50, love:{ color: "blue", sport: "football" } }; const obj2 = { other: "cat" }; const obj3 = { car: "Benz" };
const compose = Object.assign(obj, obj2, obj3);
const obj = { firstname:"John", lastname:"Doe", age:50, love:{ color: "blue", sport: "football" } }; const deepClone = JSON.parse(JSON.stringify(obj));
|
Object.entries(obj)
Object.entries(obj) 返回对象的key和value组成的数组
Object.entries(obj) 方法返回一个对象key和value键值对组成的数组
1 2 3
| const obj = { foo: 'bar', baz: 42 }; console.log(Object.entries(obj));
|
obj.hasOwnProperty()
obj.hasOwnProperty() 方法判断对象中属性是否存在
1 2 3 4 5 6 7 8 9 10
| const obj = { firstname:"John", lastname:"Doe", age:50, love:{ color: "blue", sport: "football" } }; console.log(obj.hasOwnProperty('love'))
|