Object.keys()和Object.values()小记
Object.keys()把对象属性转为数组
// 获取对象的属性
let person={ name:'一只流浪的kk', age:20, eat:function(){} } console.log(Object.keys(person));// ['name','age','eat']
//获取数组的索引
let arr=[1,2,3,4,5]; console.log(Object.keys(arr));//['0','1','2','3','4','5']
//处理字符串,返回索引值数组
let str='hello'; console.log(Object.keys(str));//['0','1','2','3','4']
let person={ name:'一只流浪的kk', age:18, eat:function(){ } } Object.keys(person).map((key)=>{ person[key];//获得属性对应的值,可以进行其它处理 })
// Object.values把对像的值转为数组
var obj = {
10: 'a',
1: 'b',
2: 'c'
};
console.log(Object.values(obj));
// ['b', 'c', 'a']
var obj1 = {
0: 'a',
1: 'b',
2: 'c'
};
console.log(Object.values(obj1));
// ['a', 'b', 'c']