getEdgeEvent(edge) { let target, source; for (let item of this.$store.state.graphInfo.getCells()) { if (item.id == edge.target.cell) { target = item; } if (item.id == edge.source.cell) { source = item; } } return { target, source, }; }, 进行优化
时间: 2023-07-16 10:17:09 浏览: 35
首先,建议使用对象解构来简化代码:
```
getEdgeEvent(edge) {
const { cells } = this.$store.state.graphInfo;
let target, source;
for (let item of cells) {
if (item.id === edge.target.cell) {
target = item;
}
if (item.id === edge.source.cell) {
source = item;
}
}
return { target, source };
}
```
其次,可以使用 `Array.find()` 方法来查找目标节点,这样可以使代码更简洁:
```
getEdgeEvent(edge) {
const { cells } = this.$store.state.graphInfo;
const target = cells.find(item => item.id === edge.target.cell);
const source = cells.find(item => item.id === edge.source.cell);
return { target, source };
}
```
使用 `Array.find()` 方法可以避免使用循环,提高代码的可读性和性能。
相关问题
Worksheet.getCells().insertColumns(columnIndex,totalColumns,updateReference
Worksheet.getCells().insertColumns(columnIndex,totalColumns,updateReference)可用于在特定的工作表中插入指定数量的列,并根据需要更新参考。
Row row = worksheet.getCells().getRow(4); 如何编译一行中每列数据
一旦你获取了一行的数据,你可以使用 `Row` 对象的 `getCell` 方法来访问该行中的每个单元格。例如,下面的代码显示了如何循环遍历第 5 行中的所有单元格,并打印它们的值:
```java
//Assuming your workbook is already loaded into a variable called workbook
Worksheet worksheet = workbook.getWorksheets().get(0);
Row row = worksheet.getCells().getRow(4); // Get the 5th row
for (int i = 0; i < row.getLastCellNum(); i++) {
Cell cell = row.getCell(i); // Get the cell at index i
System.out.println(cell.getValue()); // Print the cell value
}
```
在上面的代码中,我们使用 `getLastCellNum` 方法来获取该行中的单元格数量,然后使用一个 for 循环遍历每个单元格。在循环中,我们使用 `getCell` 方法来获取每个单元格,并使用 `getValue` 方法来获取单元格的值。
请注意,如果你的 Excel 文件中有空单元格,则 `getLastCellNum` 方法返回的值可能会大于实际存在的单元格数量。因此,你需要检查每个单元格是否为空,然后再访问其值。你可以使用 `Cell` 对象的 `isNull` 方法来判断单元格是否为空。