JavaScript中的Object.keys()方法
參數(shù): 要返回其枚舉自身屬性的對象
返回值: 一個表示給定對象的所有可枚舉屬性的字符串數(shù)組
問題:如何獲取到對象中的所有的鍵的數(shù)組(Object)
// 定義一個數(shù)組
var info = {"name":"張三"胸私,"age":18,"sex":"男"}
console.log(Object.keys(info)); // ["name","age","sex"]
問題:對于數(shù)組中會返回什么(Array)
// 定義一個數(shù)組
var animal = ["dog","cat"你辣,"tiger"]
// 調用Objects.keys() 返回一個迭代器
console.log(Objects.keys(animal)) // ["0","1","2"]
// 注意:返回字符串的數(shù)組
問題:當處理字符串的時候會返回什么京郑?(String)
// 定義一個字符串
var myStr = "hello word"
console.log(Objects.keys(myStr)))
// ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
區(qū)分數(shù)組中的keys()方法
var animal = ['dog','cat','tiger']; //
var animalIterator = animal.keys(); //
console.log(animalIterator); // Array Iterator {}
console.log(animalIterator.next()); // {value: 0, done: false}
console.log(animalIterator.next()); // {value: 1, done: false}
console.log(animalIterator.next()); // {value: 2, done: false}
console.log(animalIterator.next()); // {value: undefined, done: true}
注意:Object.values()和Object.keys()是相反的操作,把一個對象的值轉換為數(shù)組葫掉。