const wholeList = ["pm25", "aqi", "pm25/pm10", "co2/no2"]; const divisor = ["pm25/pm10", "co2/no2"]; 使用js去除wholeList数组中与divisor的重复项
时间: 2023-11-03 07:30:39 浏览: 139
可以使用filter和includes方法来解决这个问题,具体代码如下:
const wholeList = ["pm25", "aqi", "pm25/pm10", "co2/no2"];
const divisor = ["pm25/pm10", "co2/no2"];
const filteredList = wholeList.filter(item => !divisor.includes(item));
console.log(filteredList);
输出的结果是:["pm25", "aqi"],其中pm25/pm10和co2/no2被在wholeList数组中被过滤掉了。
相关问题
const divisor = ["aqi", "pm25", "pm25/pm10", "co2/no2"]; 使用js删除divisor中的“pm25”
好的,我理解了您的问题。要删除数组中的特定元素,您可以使用JavaScript中的splice()方法。以下是删除divisor数组中“pm25”的代码:
```
const divisor = ["aqi", "pm25", "pm25/pm10", "co2/no2"];
const index = divisor.indexOf("pm25");
if (index > -1) {
divisor.splice(index, 1);
}
```
以上代码将会输出 ["aqi", "pm25/pm10", "co2/no2"],即已经将“pm25”从数组中删除了。希望这能够帮助您!
阅读全文