arr=[3,5],brr=[{name:1},{name:1},{name:1},{name:2},{name:8},{name:9},{name:6}];找出arr元素值作为brr的索引值生成的新数组
时间: 2023-11-01 20:22:52 浏览: 78
根据题意,我们可以使用一个循环遍历 brr 数组,并将每个对象的 name 属性作为索引,将该对象添加到新数组中。最后返回新数组即可。具体的实现方式如下所示:
```python
def generate_new_array(arr, brr):
result = []
for i in arr:
index = i
if index < len(brr):
result.append(brr[index])
return result
```
例如,当 `arr=[3,5],brr=[{name:1},{name:1},{name:1},{name:2},{name:8},{name:9},{name:6}]` 时,调用 `generate_new_array(arr, brr)` 的结果为 `[{name:2}, {name:9}]`。
相关问题
arr=[3,5],brr=[{name:1},{name:1},{name:1},{name:2},{name:8},{name:9},{name:6}];js方法找出arr元素值作为brr的索引值生成的新数组
可以使用 JavaScript 来实现相同的功能。具体的实现方式如下所示:
```javascript
function generateNewArray(arr, brr) {
let result = [];
for (let i of arr) {
let index = i;
if (index < brr.length) {
result.push(brr[index]);
}
}
return result;
}
```
例如,当 `arr=[3,5],brr=[{name:1},{name:1},{name:1},{name:2},{name:8},{name:9},{name:6}]` 时,调用 `generateNewArray(arr, brr)` 的结果为 `[{name:2}, {name:9}]`。
注意,在 JavaScript 中,对象的属性名需要用引号括起来,所以在 brr 数组中的对象需要改成 `{name: "1"}` 的形式。
let data = [ { name: "张三", nephrogenesis: '["id1-2","id1-3","id1-4"]', diagnose: "[1,2,3]" }, { name: "李三", nephrogenesis: '["id1-2","id1-3","id1-5"]', diagnose: "[2,3,4]" }, { name: "王三", nephrogenesis: '["id1-2","id1-3","id1-6"]', diagnose: "[1,4]" }, { name: "孙三", nephrogenesis: '["id1-2","id1-3","id2-1"]', diagnose: "[2,3]" } ]; let nephrogenesisListTwo = [ { title: "感染性疾病", value: "id1" }, { title: "梅毒", value: "id1-1" }, { title: "败血症", value: "id1-2" }, { title: "艾滋病", value: "id1-3" }, { title: "慢性丙型病毒性肝炎", value: "id1-4" }, { title: "慢性乙型病毒性肝炎", value: "id1-5" }, { title: "流行性出血热", value: "id1-6" }, { title: "泌尿系肿瘤", value: "id2" }, { title: "前列腺癌", value: "id2-1" }, { title: "右肾癌", value: "id2-2" } ]; let diagnoseList = { 1: "慢性肾功能不全", 2: "肾病综合征", 3: "高尿酸血症", 4: "冠心病", 5: "多囊肾" }; data.map(al => { if (al.nephrogenesis) { let arr = JSON.parse(al.nephrogenesis); let str = ""; arr.map(result => { for (let i = 0; i < nephrogenesisListTwo.length; i++) { if (nephrogenesisListTwo[i].value === result) { str += nephrogenesisListTwo[i].title + "、"; } } }); al.nephrogenesisName = str.replace(/、$/, ""); } if (al.diagnose) { let brr = JSON.parse(al.diagnose); let texts = ""; brr.map(resul => { texts += diagnoseList[resul] + "、"; }); al.diagnoseName = texts.replace(/、$/, ""); } }); 代码优化
你可以使用 `forEach` 方法来替代 `map` 方法,并使用 `filter` 方法来查找匹配的项。此外,你可以使用模板字符串来简化代码。以下是优化后的代码:
```javascript
data.forEach(al => {
if (al.nephrogenesis) {
let arr = JSON.parse(al.nephrogenesis);
let str = arr
.map(result => nephrogenesisListTwo.find(item => item.value === result)?.title)
.filter(Boolean)
.join("、");
al.nephrogenesisName = str;
}
if (al.diagnose) {
let brr = JSON.parse(al.diagnose);
let texts = brr
.map(result => diagnoseList[result])
.filter(Boolean)
.join("、");
al.diagnoseName = texts;
}
});
```
通过使用 `forEach` 方法和箭头函数,你可以避免使用 for 循环来遍历数组。然后使用 `find` 方法来查找匹配的项,并使用 `filter` 方法过滤掉可能的空值。最后使用 `join` 方法将结果连接为一个字符串。这样可以更简洁地实现相同的功能。
阅读全文