试图最小化js代码并希望提高js的性能速度
trying to minimalize the js code and want to increase the performance speed in js
我尽量避免写行数,因为所有字段的响应都相同,所以我尝试使用 for 循环对字段进行分类['mandatory','optional']但我仍然需要为循环写两次。是否可以在不影响性能的情况下一次性更高效地编写。
//mandatory
obj.required['test1' + number] = true;
obj.visible['test1' + number] = true;
obj.required['test2' + number] = true;
obj.visible['test2' + number] = true;
obj.required['test3' + number] = true;
obj.visible['test3' + number] = true;
//optional
obj.visible['test4' + number] = true;
obj.visible['test5' + number] = true;
尝试使用 for 循环:
var mandatoryField = ['test1', 'test2', 'test3'];
var optionalField = ['test4', 'test5'];
for(var i=0; i<mandatoryField.length; i++) {
obj.required[madatoryField[i] + number] = true;
obj.visible[madatoryField[i] + number] = true;
}
像这样的事情将帮助您在一次迭代中实现这一目标。
var mandatoryField = ['test1', 'test2', 'test3'];
var optionalField = ['test4', 'test5'];
const limit = new Array(Math.max(mandatoryField.length, optionalField.length)).fill(0);
limit.forEach((item, index) => {
if(mandatoryField[index]) {
obj.required[mandatoryField[index] + number] = true;
obj.visible[mandatoryField[index] + number] = true;
}
if(optionalField[index]) {
obj.visible[optionalField[index] + number] = true;
}
})
你能改变字段的结构吗?
const obj = {
visible: {
test1: false,
test2: false,
test3: false,
test4: false,
test5: false,
test6: false,
test7: false,
test8: false,
},
required: {
test1: false,
test2: false,
test3: false,
test4: false,
test5: false,
test6: false,
test7: false,
test8: false,
},
};
const fields = [
{ field: 'test1', required: true },
{ field: 'test2', required: true },
{ field: 'test3', required: true },
{ field: 'test4' },
{ field: 'test5' },
];
fields.forEach(({ field, required }) => {
const key = field; // `${field}${number}`;
obj.visible[key] = true;
if (required) {
obj.required[key] = true;
}
});
console.log(obj);
我尽量避免写行数,因为所有字段的响应都相同,所以我尝试使用 for 循环对字段进行分类['mandatory','optional']但我仍然需要为循环写两次。是否可以在不影响性能的情况下一次性更高效地编写。
//mandatory
obj.required['test1' + number] = true;
obj.visible['test1' + number] = true;
obj.required['test2' + number] = true;
obj.visible['test2' + number] = true;
obj.required['test3' + number] = true;
obj.visible['test3' + number] = true;
//optional
obj.visible['test4' + number] = true;
obj.visible['test5' + number] = true;
尝试使用 for 循环:
var mandatoryField = ['test1', 'test2', 'test3'];
var optionalField = ['test4', 'test5'];
for(var i=0; i<mandatoryField.length; i++) {
obj.required[madatoryField[i] + number] = true;
obj.visible[madatoryField[i] + number] = true;
}
像这样的事情将帮助您在一次迭代中实现这一目标。
var mandatoryField = ['test1', 'test2', 'test3'];
var optionalField = ['test4', 'test5'];
const limit = new Array(Math.max(mandatoryField.length, optionalField.length)).fill(0);
limit.forEach((item, index) => {
if(mandatoryField[index]) {
obj.required[mandatoryField[index] + number] = true;
obj.visible[mandatoryField[index] + number] = true;
}
if(optionalField[index]) {
obj.visible[optionalField[index] + number] = true;
}
})
你能改变字段的结构吗?
const obj = {
visible: {
test1: false,
test2: false,
test3: false,
test4: false,
test5: false,
test6: false,
test7: false,
test8: false,
},
required: {
test1: false,
test2: false,
test3: false,
test4: false,
test5: false,
test6: false,
test7: false,
test8: false,
},
};
const fields = [
{ field: 'test1', required: true },
{ field: 'test2', required: true },
{ field: 'test3', required: true },
{ field: 'test4' },
{ field: 'test5' },
];
fields.forEach(({ field, required }) => {
const key = field; // `${field}${number}`;
obj.visible[key] = true;
if (required) {
obj.required[key] = true;
}
});
console.log(obj);