4种JavaScript中不同迭代对象的方法
WEB前端开发社区 2021-11-02
const obj1 = { test1: 'atit', test2: 53, test3: false,};console.log(Object.values(obj1));// expected output: Array ["atit", 53, false]
const obj1 = { test1: 'atit', test2: 53, test3: false,};console.log(Object.keys(obj1));// expected output: Array ["test1", "test2", "test3"]
const obj1 = { test1: 'atit', test2: 53, test3: false,};for (let [key, value] of Object.entries(obj1)) { console.log(key, value);}//test1 atit//test2 53//test3 false
for (let key in obj1) { if (obj1.hasOwnProperty(key)) { console.log(key, obj1[key]); }}//test1 atit//test2 53//test3 false
const test = new Map([ ['atit', '51'], ['patel', 52]]);const obj1 = Object.fromEntries(test);console.log(obj1);// expected output: Object { atit: 51, patel: 52 }
赞 (0)