12345678_0001
2023-04-27 16:27:49
angular我在products和中有兩個數(shù)組productsGreen。其中 product 包含所有產(chǎn)品,而 productsGreen 只有成功產(chǎn)品。當uncheck green checkbox我想productsGreen從products. 我已經(jīng)看到很多答案,他們正在使用循環(huán)并編寫多行代碼。是否可以在一行代碼中完成,例如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條經(jīng)驗 獲得超7個贊
您可以按屬性過濾對象數(shù)組?;舅悸罚?/p>
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
提交
取消