类数组转换成数组的N种方式

tech2023-12-29  88

类数组:一组通过数字索引的对象,字符串也是类数组

function transfrom(){ const args = arguments; // 1. for循环 const res = []; for (var i = 0; i < args.length; i++) { res.push(args[i]); } // 2. Object.values const res = Object.values(args); // 3. for of const res = []; for (var key of args) { res.push(key); } // 4. for in const res = []; for (var key in args) { res.push(args[key]); } // 5. Array.from const res = Array.from(args); // 6. slice const res = Array.prototype.slice.call(args); // 7. 扩展运算符 console.log(...args); console.log(res, Array.isArray(res)); }
最新回复(0)