一、语法 Object.keys(obj) 参数:需要返回自身属性的对象 返回值:一个数组 Object.values(obj) 参数:需要返回自身属性的对象 返回值:一个数组
二、当obj是对象 let people={name:‘张三’,age:23,sex:‘男’} Object.keys(people) 返回的是: ["name",“age”,"sex"] (返回的是属性组成的数组) Object.values(people) 返回的是: ["张三" ,"23 ","男 "] (返回的是属性值组成的数组)
三、当obj是数组 let a=[1,2,3,4] Object.keys(a) 返回的是:[" 0"," 1","2 "," 3"] (返回的是索引组成的数组) Object.values(a) 返回的是:[" 1"," 2","3 "," 4"] (返回的是数值组成的数组)
四、当obj是字符串 let a=‘dfsdfga’ Object.keys(a) 返回的是["0 "," 1", "2 ", " 3", " 4","5 " ,"6 "] (返回的是索引组成的数组) Objec.values(a) 返回的是["d ", "f ","s "," d"," f","g "," a"] (返回的是元素组成的数组)
五、常用的技巧 1、 let person = {name:"张三",age:25,address:"深圳",getName:function(){}} Object.keys(person).map((a)=>{ person[a] // 获取到属性对应的值,做一些处理}) 2、 将字符串或者对象转换成数组,需要想到这两种方式