Object.keys()
我的简单理解:将返回key组成一个数组
定义:返回一个由一个给定对象的自身可枚举属性组成的数组,数组中属性名的排列顺序和正常循环遍历该对象时返回的顺序一致 。
//字符串 let str = "abcd字符串" console.log(Object.keys(str)) // ["0", "1", "2", "3", "4", "5", "6"] // 数组 var arr = ['a', 'b', 'c']; console.log(Object.keys(arr)); //['0', '1', '2'] // 对象 var obj = { 0: 'a', 1: 'b', 2: 'c' }; console.log(Object.keys(obj)); //['0', '1', '2'] // array like object with random key ordering var anObj = { 100: 'a', 2: 'b', 7: 'c' }; console.log(Object.keys(anObj)); //['2', '7', '100'] // getFoo is a property which isn't enumerable var myObj = Object.create({}, { getFoo: { value: function () { return this.foo; } } }); myObj.foo = 1; console.log(Object.keys(myObj)); // ['foo']
Object.values()
我的理解:将返回的value组成一个数组
定义:Object.values()方法返回一个给定对象自身的所有可枚举属性值的数组,值的顺序与使用for…in循环的顺序相同 ( 区别在于 for-in 循环枚举原型链中的属性 )。
let str = “abcd字符串”
console.log(Object.values(str)) //[“a”, “b”, “c”, “d”, “字”, “符”, “串”]
var obj = { foo: ‘bar’, baz: 42 };
console.log(Object.values(obj)); // [‘bar’, 42]
//对象
var obj = { 0: 'a', 1: 'b', 2: 'c' }; console.log(Object.values(obj)); // ['a', 'b', 'c'] // array like object with random key ordering // when we use numeric keys, the value returned in a numerical order according to the keys var an_obj = { 100: 'a', 2: 'b', 7: 'c' }; console.log(Object.values(an_obj)); // ['b', 'c', 'a'] // getFoo is property which isn't enumerable var my_obj = Object.create({}, { getFoo: { value: function() { return this.foo; } } }); my_obj.foo = 'bar'; console.log(Object.values(my_obj)); // ['bar'] // non-object argument will be coerced to an object console.log(Object.values('foo')); // ['f', 'o', 'o']
借鉴:https://blog.csdn.net/YouZi_X/article/details/107768270