List<Map<String,Object>>类型的参数,前端怎么循环取出其中指定id的数据
时间: 2023-11-11 15:58:24 浏览: 66
假设List<Map<String,Object>>的结构如下:
```java
List<Map<String,Object>> list = new ArrayList<>();
Map<String,Object> map1 = new HashMap<>();
map1.put("id",1);
map1.put("name","张三");
list.add(map1);
Map<String,Object> map2 = new HashMap<>();
map2.put("id",2);
map2.put("name","李四");
list.add(map2);
Map<String,Object> map3 = new HashMap<>();
map3.put("id",3);
map3.put("name","王五");
list.add(map3);
```
如果要获取id为2的数据,可以使用以下代码:
```javascript
var targetId = 2;
for(var i=0;i<list.length;i++){
if(list[i].id == targetId){
console.log(list[i]);
}
}
```
或者使用ES6的find方法:
```javascript
var targetId = 2;
var result = list.find(item => item.id === targetId);
console.log(result);
```
此时会输出:
```
{id: 2, name: "李四"}
```
阅读全文