// 1.处理对象,返回可枚举的属性数组
let person = {name:"张三",age:25,address:"深圳",getName:function(){}}
const test = Object.keys(person)
console.log(test) // ["name", "age", "address", "getName"]
// 2.处理数组,返回索引值数组
let arr = [1,2,3,4,5,6]
const test1 = Object.keys(arr)
console.log(test1) // ["0", "1", "2", "3", "4", "5"]
// 3.处理字符串,返回索引值数组
let str = 'saasd'
const test3 = Object.keys(str)
console.log(test3) // ["0", "1", "2", "3", "4"]
// 4.常用技巧
const arr4 = []
Object.keys(person).map(item => {
arr4.push(person[item])
})
// person.forEach(item => {
// arr4.push(person[item])
// }); // 报错 forEach是遍历数组对象的
console.log(arr4) // ["张三", 25, "深圳", ƒ]
// Object.values()和Object.keys()是相反的操作,把一个对象的值转换为数组
const aaa = Object.values(person)
console.log(aaa) // ["张三", 25, "深圳", ƒ]
Object.keys
最新推荐文章于 2024-06-07 21:58:32 发布