rowNo = isSubList() ? (i > 1 ? rowNo + 1 : rowNo + i) : i + 1 + rownum - startNo;将这个嵌套的三元运算提取到一个独立的语句中。
时间: 2024-03-21 07:38:12 浏览: 83
可以将嵌套的三元运算提取到一个独立的语句中,如下所示:
```java
if (isSubList()) {
if (i > 1) {
rowNo = rowNo + 1;
} else {
rowNo = rowNo + i;
}
} else {
rowNo = i + 1 + rownum - startNo;
}
```
这段代码与原来的代码等价,但更易读懂。首先,我们使用了 if 语句替换了三元运算符,使代码更加清晰。其次,我们将嵌套的三元运算符拆分成了两个 if 语句,避免了代码过于复杂的情况。最后,我们为每个分支分别设置了具体的操作,使代码更加直观。
相关问题
result= [ {rowNo: "R001", columnValueList:[{ C001: null }, { C002: "123" }] }] const sheetId=[{name:'C101',id:'C101-JBQKTJB'},{name:'C201',id:'C201-ZCFZTJB'},{name:'C202',id:'C202-JYDSTJB'},{name:'C203',id:'C203-LRTJB'}, {name:'C30701',id:'C30701-ZYYWFJYDSTJB'},{name:'C30702',id:'C30702-ZYYWBDTJB'},{name:'C30703',id:'C30703-YWFDQTJB'},{name:'C30704',id:'C30704-RZTXTJB'}, {name:'C30705',id:'C30705-YWSGJGTJB'},{name:'C407',id:'C407-HYFXTJB'},{name:'C408',id:'C408-ZCFZSYQXTJB'},{name:'C409',id:'C409-SDKHJZDTJB'}, {name:'C410',id:'C410-ZDWJGLFTJB'},{name:'A001',id:'A001-JGXXHZB'},{name:'A002',id:'A002-JDJGXXB'},{name:'A003',id:'A003-SJJHB'},{name:'A004',id:'A004-SJTJB'}]这两个数组对象转为[{"reportNo": "C101-JBQKTJB", "rowValueList": [{ "rowNo": "R001", "columnValueList": [{ "C001": "123" }, { "C002": "456" }] }, { "rowNo": "R002", "columnValueList": [{ "C001": "433124" }, { "C002": "451234126" }] } ] }]这种JSON格式
你可以使用以下代码实现将result和sheetId转换为所需的JSON格式:
```python
output = []
for item in sheetId:
reportNo = item['id']
rowValueList = []
for row in result:
rowNo = row['rowNo']
columnValueList = []
for column in row['columnValueList']:
columnName = list(column.keys())[0]
columnValue = column[columnName]
if columnValue is not None:
columnValueList.append({columnName: columnValue})
rowValueList.append({'rowNo': rowNo, 'columnValueList': columnValueList})
output.append({'reportNo': reportNo, 'rowValueList': rowValueList})
```
这个代码使用了三层循环,第一层循环遍历sheetId数组中的每个元素,获取reportNo。第二层循环遍历result数组中的每个元素,获取rowNo。第三层循环遍历columnValueList数组中的每个元素,获取columnName和columnValue,如果columnValue不为null,则将其加入到新的columnValueList数组中。最终将reportNo、rowNo和columnValueList整合在一起,加入到output数组中。
注意,由于result数组中的每个元素的columnValueList属性是一个对象数组,需要使用list()函数获取其中的第一个属性名,即columnName。
最终,output数组中的元素就是转换后的JSON格式。
javascript怎么将[{"reportNo":"C101-JBQKTJB","rowValueList":[{"columnValueList":[{"C001":"苏州资产管理有限公司"}],"rowNo":"R001"},{"columnValueList":[{"C001":"91320500MA1ML9WL32"}],"rowNo":"R002"},{"columnValueList":[{"C001":"苏州高新区邓尉路105号"}],"rowNo":"R003"},{"columnValueList":[{"C001":"320505"}],"rowNo":"R004"},{"columnValueList":[{"C001":"苏州高新区邓尉路105号"}],"rowNo":"R005"},{"columnValueList":[{"C001":"地方资产管理公司"}],"rowNo":"R006"},{"columnValueList":[{"C001":"110 国有控股"}],"rowNo":"R007"},{"columnValueList":[{"C001":63}],"rowNo":"R008"},{"columnValueList":[{"C001":0}],"rowNo":"R009"},{"columnValueList":[{"C001":5}],"rowNo":"R010"},{"columnValueList":[{"C001":2676090.86}],"rowNo":"R011"},{"columnValueList":[{"C001":1960314.72}],"rowNo":"R012"},{"columnValueList":[{"C001":572000}],"rowNo":"R013"},{"columnValueList":[{"C001":572000}],"rowNo":"R014"},{"columnValueList":[{"C001":"苏州市财政局"}],"rowNo":"R015"},{"columnValueList":[{"C001":22.5}],"rowNo":"R016"},{"columnValueList":[{"C001":"东吴创新资本管理有限责任公司"}],"rowNo":"R017"},{"columnValueList":[{"C001":12}],"rowNo":"R018"},{"columnValueList":[{"C001":"东吴证券股份有限公司"}],"rowNo":"R019"},{"columnValueList":[{"C001":8}],"rowNo":"R020"},{"columnValueList":[{"C001":"苏州国际发展集团有限公司"}],"rowNo":"R021"},{"columnValueList":[{"C001":7.5}],"rowNo":"R022"},{"columnValueList":[{"C001":"苏州工业园区经济发展有限公司"}],"rowNo":"R023"},{"columnValueList":[{"C001":7.5}],"rowNo":"R024"},{"columnValueList":[{"C001":"李瑞良"}],"rowNo":"R025"},{"columnValueList":[{"C001":"18625272017"}],"rowNo":"R026"},{"columnValueList":[{"C001":"马静"}],"rowNo":"R027"},{"columnValueList":[{"C001":"68786950"}],"rowNo":"R028"}],"reportType":"0","informMonth":"2023-05"}这里的"informMonth"排到最前面,reportType排到informMonth后面
你可以使用JavaScript中的sort()方法来对这个数组进行排序。假设这个数组是存储在变量data中的,你可以按照以下方式进行排序:
```javascript
data.sort((a, b) => {
if (a.informMonth < b.informMonth) {
return -1;
}
if (a.informMonth > b.informMonth) {
return 1;
}
if (a.reportType < b.reportType) {
return -1;
}
if (a.reportType > b.reportType) {
return 1;
}
return 0;
});
```
这里用了箭头函数和多个if语句来比较两个对象的informMonth和reportType属性。如果a应该排在b前面,返回-1;如果a应该排在b后面,返回1;如果a和b相等,返回0。最终sort()方法会根据这个比较函数的返回值来对数组进行排序。
阅读全文