let memo = [] as any if (baseSetting.value.memo_source === 1) { memo = orderMemoList.value } else if (baseSetting.value.memo_source === 2) { invoiceDetail.value.forEach(item => { memo.push(item.memo) }) } else { memo = orderMemoList.value invoiceDetail.value.forEach(item => { memo.push(item.memo) }) } memo = memo.filter(iv => iv !== null) if (baseSetting.value.memo_merge === 1) { memo = Array.from(new Set(memo)) } memo = memo.join(',') 优化这段代码,防止orerMeomoList的值被修改
时间: 2024-03-27 17:39:30 浏览: 89
Hex-Value-Seach.zip_delphi memo_hex delphi_memo
可以使用 `slice()` 方法创建一个新的数组,而不是直接引用 `orderMemoList.value`,从而避免原始数组被修改。同时,可以使用 `map()` 方法创建一个新的数组,而不是对原始数组进行遍历和修改。下面是优化后的代码:
```
let memo: string[] = [];
if (baseSetting.value.memo_source === 1) {
memo = orderMemoList.value.slice();
} else if (baseSetting.value.memo_source === 2) {
memo = invoiceDetail.value.map(item => item.memo);
} else {
memo = orderMemoList.value.slice().concat(invoiceDetail.value.map(item => item.memo));
}
memo = memo.filter(iv => iv !== null);
if (baseSetting.value.memo_merge === 1) {
memo = Array.from(new Set(memo));
}
memo = memo.join(',');
```
这样修改之后,`orderMemoList.value` 的值就不会被修改了。
阅读全文