angular我在products和中有两个数组productsGreen。其中 product 包含所有产品,而 productsGreen 只有成功产品。当uncheck green checkbox我想productsGreen从products. 我已经看到很多答案,他们正在使用循环并编写多行代码。是否可以在一行代码中完成,例如products.remove(productsGreen).这是我的阵列products{ "color": "green", "loc": "L1"},{ "color": "green", "loc": "L2"},{ "color": "red", "loc": "L5"},{ "color": "red", "loc": "L6"},这是我的阵列productsGreen{ "color": "green", "loc": "L1"},{ "color": "green", "loc": "L2"},我需要输出{ "color": "red", "loc": "L5"},{ "color": "red", "loc": "L6"},编辑 1:这是我的代码OnChangeGreen($event){if(this.CheckboxGreen==true){ this.CheckboxGreen=false; this.chartData.pop(this.mapDatagreen) this.mapImageSeries.data=this.chartData; this.chartData.validateData();}}
1 回答
千巷猫影
TA贡献1829条经验 获得超7个赞
您可以按属性过滤对象数组。基本思路:
var data = [{
"color": "green",
"loc": "L1"
},
{
"color": "green",
"loc": "L2"
},
{
"color": "red",
"loc": "L5"
},
{
"color": "red",
"loc": "L6"
}
];
const selectItems = filters =>
data.filter(item => filters.includes(item.color))
console.log("red", selectItems(["red"]));
console.log("green", selectItems(["green"]));
console.log("red green", selectItems(["red", "green"]));
console.log("blue", selectItems(["blue"]));
添加回答
举报
0/150
提交
取消