DM
Size: a a a
DM
Ю
K
ИМ
ИМ
ИМ
ЯС
module.exports = {И вот такой код
map: {
'src/components/inputs': {
index: 'index',
button: 'react-component',
textarea: 'react-component',
input: 'react-component',
'input-select': 'react-component',
},
'src/components/modals': {
base: 'react-component',
confirm: 'react-component',
'confirm-remove': 'react-component',
},
'src/components/store': {
profile: 'store',
user: 'store',
users: 'store',
},
},
};
const sourceMap = require('../../settings/source-map');Как можно оптимизировать и отрефакторить код, чтобы он не был захардкоженный, но результат получал такой же, помогите пожалуйста.
Object.entries(sourceMap.map).forEach(([sourcePath, components]) => {
const componentArrIndex = [];
const componentArrReact = [];
const componentArrStore = [];
const obj = {};
Object.entries(components).forEach(([component, template]) => {
if (template === 'index') {
componentArrIndex.push(component);
return;
} else if (template === 'react-component') {
componentArrReact.push(component);
} else if (template === 'store') {
componentArrStore.push(component);
}
});
// console.log(componentArr);
Object.entries(components).forEach(([component, template]) => {
if (template === 'index') {
obj[template] = componentArrIndex;
} else if (template === 'react-component') {
obj[template] = componentArrReact;
} else if (template === 'store') {
obj[template] = componentArrStore;
}
});
console.log(obj);
});
{ index: [ 'index' ],
'react-component': [ 'button', 'textarea', 'input', 'input-select' ] }
{ 'react-component': [ 'base', 'confirm', 'confirm-remove' ] }
{ store: [ 'profile', 'user', 'users' ] }
RR
module.exports = {И вот такой код
map: {
'src/components/inputs': {
index: 'index',
button: 'react-component',
textarea: 'react-component',
input: 'react-component',
'input-select': 'react-component',
},
'src/components/modals': {
base: 'react-component',
confirm: 'react-component',
'confirm-remove': 'react-component',
},
'src/components/store': {
profile: 'store',
user: 'store',
users: 'store',
},
},
};
const sourceMap = require('../../settings/source-map');Как можно оптимизировать и отрефакторить код, чтобы он не был захардкоженный, но результат получал такой же, помогите пожалуйста.
Object.entries(sourceMap.map).forEach(([sourcePath, components]) => {
const componentArrIndex = [];
const componentArrReact = [];
const componentArrStore = [];
const obj = {};
Object.entries(components).forEach(([component, template]) => {
if (template === 'index') {
componentArrIndex.push(component);
return;
} else if (template === 'react-component') {
componentArrReact.push(component);
} else if (template === 'store') {
componentArrStore.push(component);
}
});
// console.log(componentArr);
Object.entries(components).forEach(([component, template]) => {
if (template === 'index') {
obj[template] = componentArrIndex;
} else if (template === 'react-component') {
obj[template] = componentArrReact;
} else if (template === 'store') {
obj[template] = componentArrStore;
}
});
console.log(obj);
});
{ index: [ 'index' ],
'react-component': [ 'button', 'textarea', 'input', 'input-select' ] }
{ 'react-component': [ 'base', 'confirm', 'confirm-remove' ] }
{ store: [ 'profile', 'user', 'users' ] }
DR
o
o
Ю
o
W
o
o