查漏补缺——说说Object.keys().forEach()
问题
如题所示
答案
相关源码:
const files = require.context('.', true, /\.js$/);
files.keys().forEach((key) => {
if (key === './index.js') return;
files(key).default.forEach(item => {
let {url,type,response} = item;
// 注册所有的mock服务
Mock.mock(new RegExp('^' + url), type, XHR2ExpressReqWrap(response));
})
});
【前端】Object.keys()的使用方法及数组遍历,Object.keys(object).forEach(e => {您的代码})
根据上面,files.keys().forEach()
会将files文件对象的属性名存放在一个数组里面,然后使用forEach去遍历这个数组。