在满足某些条件时,我需要通过客户端脚本显示alert()
。但是我无法打印记录号,这将是对用户的关键信息,这些特定记录正在导致系统显示警报。
我做了一个for循环,在其中将docNo
(记录ID)的值压入名为docNoArr
的数组中。
然后在另一个for循环中,我使用在docNoArr
数组中推送的值触发了警报。
谁能建议我一个更好的方法来优化代码,使其不断重复。我的老师对DRY说:不要重复自己。但是这里我卡住了,就像不重复一样。
我的代码:
var docNoArr = [];
/* 1st For Loop */
for (var e = 0; e < count; e++) {
isApplied = rec.getSublistValue({
sublistId: subid,
fieldId: reqid,
line: e
});
if (isApplied == true) {
rectype = rec.getSublistValue({
sublistId: subid,
fieldId: 'custpage_am_rectype',
line: e
});
if (rectype == 'qcs') {
var supplierFld = rec.getValue({ fieldId: 'custpage_am_vendor' });
var docNoFld = rec.getSublistValue({
sublistId: subid,
fieldId: 'custpage_am_docnum',
line: e
});
var recomdSupplier = rec.getSublistValue({
sublistId: subid,
fieldId: 'custpage_am_supplier',
line: e
})
if (recomdSupplier != supplierFld) {
docNoArr.push(docNoFld);
}
}
}
}
/* 2nd For Loop */
for (var ee = 0; ee < count; ee++) {
isApplied1 = rec.getSublistValue({
sublistId: subid,
fieldId: reqid,
line: ee
});
if (isApplied1 == true) {
rectype1 = rec.getSublistValue({
sublistId: subid,
fieldId: 'custpage_am_rectype',
line: ee
});
if (rectype1 == 'qcs') {
var supplierFld1 = rec.getValue({ fieldId: 'custpage_am_vendor' });
var recomdSupplier1 = rec.getSublistValue({
sublistId: subid,
fieldId: 'custpage_am_supplier',
line: ee
});
if (recomdSupplier1 != supplierFld1) {
alert('Vendor selected in not recommended in ' + docNoArr + ' \nPlease enter Correct Vendor!');
return false;
}
}
}
}
答案 0 :(得分:0)
我不确定代码是做什么的,但是看起来您创建了一个新数组只是为了获取警报索引?
可以吗?
if (recomdSupplier != supplierFld) {
docNoArr.push(docNoFld);
alert('Vendor selected in not recommended in ' + docNoArr.length - 1 + ' \nPlease enter Correct Vendor!');
}
答案 1 :(得分:0)
您是否可以将记录存储在格式化的字符串中,而不是将它们放入数组中,因为它们已经从另一个循环中进行了限定?
此外,由于该问题提到了性能,因此您可能需要设置时间戳以查看哪个循环实际上为您运行得更快,forEach
与for (var e = 0; e < count; e++) {
console.time('for each');
data['results'].forEach(element => {
//do something
});
console.timeEnd('for each');
答案 2 :(得分:0)
/* 1st for loop here*/
if (docNoArr.length > 0) {
alert('Vendor selected is not recommended in ' + docNoArr.join(', ') + '.\nPlease enter Correct Vendor!');
}
/* 2nd for loop not needed */