Object.assign,Object.keys,Object.values,Object.entries

1.Object.assign用法(拼接对象,返回一个新的对象)

let obj1 = {a:1};
let obj2 = {b:2};
let obj3 = {c:3};
let obj = Object.assign({},obj1,obj2,obj3);
console.log(obj); //{a: 1, b: 2, c: 3}

2.Object.keys用法(获取对象中key值)

let obj = {a:1,b:2,c:3};
console.log(Object.keys(obj)) //["a", "b", "c"]
for(let key of Object.keys(obj)){
   console.log(key);
    //a
    //b
    //c
}

3.Object.values用法(获取对象中value值)

let obj = {a:1,b:2,c:3};
console.log(Object.values(obj)) //[1, 2, 3]
for(let value of Object.values(obj)){
   console.log(value);
    //1
    //2
    //3
}

4 .Object.entries用法(获取对象中每一个键值对,然后转换成数组)

let obj = {a:1,b:2,c:3};
console.log(Object.entries(obj)) //[['a':1], ['b':2],['c':3]]
for(let [key,value] of Object.entries(obj)){
   console.log(key,value);
    //a,1
    //b,2
    //c,3
}

你可能感兴趣的:(Object.assign,Object.keys,Object.values,Object.entries)