// 剔除数组前为空的数据 if (this.valueList[0] === null) { let endIndex = 0 for (let index = 0; index < this.valueList.length; index++) { if (this.valueList[index] || this.valueList[index] === 0) { endIndex = index break } } this.valueList = this.valueList.slice(endIndex, this.valueList.length) } // 剔除数组后为空的数据 if (this.valueList[this.valueList.length - 1] === null) { let startIndex = 0 for (let index = 0; index < this.valueList.length; index++) { if ( this.valueList[this.valueList.length - 1 - index] || this.valueList[this.valueList.length - 1 - index] === 0 ) { startIndex = this.valueList.length - 1 - index break } } this.valueList = this.valueList.slice(0, startIndex + 1) } 优化
时间: 2024-02-24 12:53:39 浏览: 29
这段代码可以进行如下优化:
```js
// 剔除数组前为空的数据
const firstNonNullIndex = this.valueList.findIndex((value) => value !== null && value !== undefined);
if (firstNonNullIndex !== -1) {
this.valueList = this.valueList.slice(firstNonNullIndex);
}
// 剔除数组后为空的数据
const lastNonNullIndex = this.valueList
.slice()
.reverse()
.findIndex((value) => value !== null && value !== undefined);
if (lastNonNullIndex !== -1) {
this.valueList = this.valueList.slice(0, this.valueList.length - lastNonNullIndex);
}
```
这里使用了 `Array.prototype.findIndex()` 和 `Array.prototype.reverse()` 方法来查找第一个非空元素的索引和最后一个非空元素的索引,然后再进行数组的截取操作。使用这种方法可以避免使用循环,代码更加简洁易懂。
相关问题
cell.Editor = ultraDropDown;报错
如果在设置 `cell.Editor` 属性时出现错误,可能是因为 `cell` 对象的数据类型不是 `UltraGridCell`。请确保您已正确引用 `Infragistics.Win.UltraWinGrid` 命名空间,并使用正确的语法创建了 `cell` 对象。以下是一个示例代码,演示如何正确设置单元格的下拉框编辑器:
```
// 创建数据源
DataTable dataTable = new DataTable();
dataTable.Columns.Add("ID", typeof(int));
dataTable.Columns.Add("Text", typeof(string));
dataTable.Rows.Add(1, "Option 1");
dataTable.Rows.Add(2, "Option 2");
dataTable.Rows.Add(3, "Option 3");
// 创建下拉框
UltraDropDown ultraDropDown = new UltraDropDown();
ultraDropDown.DataSource = dataTable;
ultraDropDown.DisplayMember = "Text";
ultraDropDown.ValueMember = "ID";
ultraDropDown.DropDownWidth = 200;
// 获取要设置下拉框的单元格
UltraGridCell cell = ultraGrid1.Rows[0].Cells["ColumnName"];
// 将下拉框作为单元格编辑器
cell.ValueList = ultraDropDown.ValueList;
cell.ValueList.DisplayMember = "Text";
cell.ValueList.ValueMember = "ID";
```
请注意,此示例使用了 `cell.ValueList` 属性来设置下拉框,而不是 `cell.Editor` 属性。这是因为 `UltraDropDown` 控件是一个 `Infragistics.Win.UltraWinGrid.UltraDropDownButton` 类型的控件,不支持直接设置为单元格的编辑器。因此,我们需要将 `UltraDropDown` 的值列表设置为单元格的值列表,并设置值列表的显示成员和值成员,以指定下拉框应该显示的文本和值。
for (Entity e : rsList) { List<String> singleRecord = new ArrayList<>(); nameList.forEach(n -> singleRecord.add(e.get(n) == null ? "" : e.get(n).toString())); if (e.getMetadata().containsKey(ChargeConst.RATE_RULE_DETAIL)) { Set<Map<String, Object>> ruleDtls = (Set<Map<String, Object>>) e.getMetadata().get(ChargeConst.RATE_RULE_DETAIL); Map<Object, Object> mergedMap = ruleDtls.stream() .flatMap(map -> map.entrySet().stream().map(entry -> new AbstractMap.SimpleEntry<>(entry.getKey(), entry.getValue() == null ? "" : entry.getValue()))) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (oldValue, newValue) -> newValue)); final List<String> finalSingleRecord = singleRecord; valueList.add(IntStream.range(0, finalSingleRecord.size()).mapToObj( i -> finalSingleRecord.get(i).equals("") && mergedMap.get(nameList.get(i)) != null ? mergedMap.get(nameList.get(i)).toString() : finalSingleRecord.get(i)) .collect(Collectors.toList())); continue; } valueList.add(singleRecord); }降低代码复杂度
可以将代码分解为多个方法来降低复杂度,例如:
1. 提取一个方法来处理每个实体记录,将其转换为一个 String 类型的 List。
```
private List<String> processEntity(Entity e, List<String> nameList) {
List<String> singleRecord = new ArrayList<>();
nameList.forEach(n -> singleRecord.add(e.get(n) == null ? "" : e.get(n).toString()));
if (e.getMetadata().containsKey(ChargeConst.RATE_RULE_DETAIL)) {
Set<Map<String, Object>> ruleDtls = (Set<Map<String, Object>>) e.getMetadata().get(ChargeConst.RATE_RULE_DETAIL);
Map<Object, Object> mergedMap = mergeRuleDetails(ruleDtls);
final List<String> finalSingleRecord = singleRecord;
return IntStream.range(0, finalSingleRecord.size())
.mapToObj(i -> finalSingleRecord.get(i).equals("") && mergedMap.get(nameList.get(i)) != null ? mergedMap.get(nameList.get(i)).toString() : finalSingleRecord.get(i))
.collect(Collectors.toList());
}
return singleRecord;
}
```
2. 提取一个方法来合并所有规则细节的 Map。
```
private Map<Object, Object> mergeRuleDetails(Set<Map<String, Object>> ruleDtls) {
return ruleDtls.stream()
.flatMap(map -> map.entrySet().stream()
.map(entry -> new AbstractMap.SimpleEntry<>(entry.getKey(), entry.getValue() == null ? "" : entry.getValue())))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (oldValue, newValue) -> newValue));
}
```
3. 将原来的主方法进行简化,使用上面的两个方法来处理每个实体记录,并将结果添加到 valueList 中。
```
for (Entity e : rsList) {
List<String> singleRecord = processEntity(e, nameList);
valueList.add(singleRecord);
}
```
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
使用中文解释ssm管理系统文件中util文件里PageHelper类解释以下代码package com.util; import java.util.ArrayList; import java.util.List; import javax.servlet.http.HttpServletRequest; public class PageHelper { public static void getPage(List<?> list, String name, List<String> nameList, List<String> valueList, int pageSize, String number, HttpServletRequest request, String method) { StringBuffer buffer = new StringBuffer(); String name2 = name.substring(0, 1).toUpperCase() + name.substring(1); String path = ""; String action = "getAll" + name2 + ".action"; if (method != null) { action = "query" + name2 + "ByCond.action"; } List<Object> objList = new ArrayList<Object>(); if (nameList != null && valueList != null) { for (int i = 0; i < nameList.size(); i++) { path += "&" + nameList.get(i) + "=" + valueList.get(i); } } int pageNumber = list.size(); int maxPage = pageNumber; if (maxPage % pageSize == 0) { maxPage = maxPage / pageSize; } else { maxPage = maxPage / pageSize + 1; } if (number == null) { number = "0"; } int start = Integer.parseInt(number) * pageSize; int over = (Integer.parseInt(number) + 1) * pageSize; int count = pageNumber - over; if (count <= 0) { over = pageNumber; } for (int i = start; i < over; i++) { Object obj = list.get(i); objList.add(obj); } buffer.append(" 共为"); buffer.append(maxPage); buffer.append("页 共有"); buffer.append(pageNumber); buffer.append("条 当前为第"); buffer.append((Integer.parseInt(number) + 1)); buffer.append("页 "); if ((Integer.parseInt(number) + 1) == 1) { buffer.append("首页"); } else { buffer.append("首页"); } buffer.append(" "); if ((Integer.parseInt(number) + 1) == 1) { buffer.append("上一页"); } else { buffer.append("上一页"); } buffer.append(" "); if (maxPage <= (Integer.parseInt(number) + 1)) { buffer.append("下一页"); } else { buffer.append("下一页"); } buffer.append(" "); if (maxPage <= (Integer.parseInt(number) + 1)) { buffer.append("尾页"); } else { buffer.append("尾页"); } String html = buffer.toString(); request.setAttribute("html", html); request.setAttribute(name + "List", objList); }
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)