JavaScript Object.entries()方法用于返回给定对象自己的可枚举属性[key,value]对的数组。属性的顺序与手动遍历对象的属性值所给出的顺序相同。
Object.entries - 语法
Object.entries(obj)
Object.entries - 参数
obj:这是要返回其可枚举属性[key,value]对的对象。
Object.entries - 返回值
此方法返回给定对象自己的可枚举属性[key,value]对的数组。
Object.entries - 浏览器支持
| Chrome | 38 |
| Edge | yes |
| Firefox | 28 |
| Opera | no |
例子1
const obj = { 10: arry, 21: barry, 23: carry }; console.log(Object.entries(obj)[2]);
输出:
["23", "carry"]
例子2
// creating an object constructor. //and assigning values to it. const obj = { 1: marrc, 2: sort, 3: carry }; //Displaying the countable property [key, value] //pairs of the object using object.entries() method. console.log(Object.entries(obj)[2]);//access obj.
输出:
["3", "carry"]
例子3
//creating an object constructor. //and assigning values to it. const obj2 = { 10: arvind, 2: rahul, 7: Ankit }; //Displaying the countable property [key, value] //pairs of the object using object.entries() method. console.log(Object.entries(obj2)[2]);
输出:
["10", "arvind"]