本文主要是介绍ES8中Object方法-使用说明,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Object
Object.entries
返回一个给定对象自身可枚举属性的键值对数组
const kv=Object.entries(obj1);
console.log(kv); //[["name","luochao"],["age",22]]const obj=Object.entries('lc');
console.log(obj)//[["0","l"],["1","c"]]
const arr=Object.entries([1,2]);
console.dir(arr);//[["0","1"],["1","2"]]const s1={3 : 'lc',1 : 'yx',"luochao": "22"
}
console.log(Object.entries(s1)) //[["1","yx"],["3","lc"],["luochao","22"]]const s1={3 : 'lc',1 : 'yx',"luochao": "22"
}
console.log(Object.entries(s1)) //[["1","yx"],["3","lc"],["luochao","22"]]const obj = { foo: 'bar', baz: 42 };
console.log(Object.entries(obj)); // [ ['foo', 'bar'], ['baz', 42] ]// array like object
const obj = { 0: 'a', 1: 'b', 2: 'c' };
console.log(Object.entries(obj)); // [ ['0', 'a'], ['1', 'b'], ['2', 'c'] ]// array like object with random key ordering
const anObj = { 100: 'a', 2: 'b', 7: 'c' };
console.log(Object.entries(anObj)); // [ ['2', 'b'], ['7', 'c'], ['100', 'a'] ]// getFoo is property which isn't enumerable
const myObj = Object.create({}, { getFoo: { value() { return this.foo; } } });
myObj.foo = 'bar';
console.log(Object.entries(myObj)); // [ ['foo', 'bar'] ]// non-object argument will be coerced to an object
console.log(Object.entries('foo')); // [ ['0', 'f'], ['1', 'o'], ['2', 'o'] ]// iterate through key-value gracefully
const obj = { a: 5, b: 7, c: 9 };
for (const [key, value] of Object.entries(obj)) {console.log(`${key} ${value}`); // "a 5", "b 7", "c 9"
}// Or, using array extras
Object.entries(obj).forEach(([key, value]) => {
console.log(`${key} ${value}`); // "a 5", "b 7", "c 9"
});
这篇关于ES8中Object方法-使用说明的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!