[{"id":49,"carModel":"","swversion":"V10_R16.10","osVersion":"","osPushTime":null,"createTime":"2023-05-31 15:56:30","createId":null,"createName":null,"updateTime":"2023-05-31 15:56:30","updateId":null,"updateName":null,"queryKeyword":null,"startPage":0,"pageSize":0,"crb":null,"ecu":"ADCU"},{"id":50,"carModel":"","swversion":"V10_R16.13","osVersion":"","osPushTime":null,"createTime":"2023-05-31 15:56:30","createId":null,"createName":null,"updateTime":"2023-05-31 15:56:30","updateId":null,"updateName":null,"queryKeyword":null,"startPage":0,"pageSize":0,"crb":null,"ecu":"ADCU"}],将数据中的carModel和swversion过滤出来并去重
时间: 2023-07-03 13:29:29 浏览: 85
可以使用JavaScript中的高阶函数`map()`和`reduce()`来实现这个功能。假设我们有一个包含多个对象的数组,每个对象都有一个属性`carModel`和一个属性`swversion`,我们需要提取所有`carModel`和`swversion`的值,并去重。
代码示例:
```javascript
const data = [
{ id: 49, carModel: "", swversion: "V10_R16.10", osVersion: "", osPushTime: null, createTime: "2023-05-31 15:56:30", createId: null, createName: null, updateTime: "2023-05-31 15:56:30", updateId: null, updateName: null, queryKeyword: null, startPage: 0, pageSize: 0, crb: null, ecu: "ADCU" },
{ id: 50, carModel: "", swversion: "V10_R16.13", osVersion: "", osPushTime: null, createTime: "2023-05-31 15:56:30", createId: null, createName: null, updateTime: "2023-05-31 15:56:30", updateId: null, updateName: null, queryKeyword: null, startPage: 0, pageSize: 0, crb: null, ecu: "ADCU" }
];
const carModels = [...new Set(data.map(item => item.carModel))];
const swVersions = [...new Set(data.map(item => item.swversion))];
console.log(carModels); // [""]
console.log(swVersions); // ["V10_R16.10", "V10_R16.13"]
```
首先使用`map()`函数将每个对象的`carModel`和`swversion`提取出来,然后使用ES6的扩展运算符和`Set`去重,得到所有的`carModel`和`swversion`数组。
阅读全文