试图从数组中删除多个对象,我做的解决方案很好并且可以工作,但是我想要的是我不想过滤两次,只想用一种方式
那么您能帮我获得最佳解决方案吗
示例
const arrList = [{v:'1',l:'label1'},{v:'2',l:'label2'},
{v:'3',l:'label3'}, {v:'4',l:'label4'},
{v:'5',l:'label5'}]
const filter1 = arrList.filter((a) => a.l !== 'label1')
const filter3 = filter1.filter((a) => a.l !== 'label3')
console.log(filter3);
答案 0 :(得分:1)
您正在使用2条语句进行过滤,第一条不等于label1
,另一条语句label3
。您可以使用AND &&
运算符将它们合并为一条语句,如下所示
const filter1 = arrList.filter((a) => a.l !== 'label1' && a.l !== 'label3')
答案 1 :(得分:1)
或者您可以使用更简单的解决方案:
arrList.filter((a) => !['label1', 'label3'].includes(a.l))
答案 2 :(得分:0)
您可以使用.list-group-item-deleted {
text-decoration: line-through
}
将逻辑语句组合成单个&&
表达式:
filter()
答案 3 :(得分:0)
尝试这样的事情!
const arrList = [{v:'1',l:'label1'},{v:'2',l:'label2'},
{v:'3',l:'label3'}, {v:'4',l:'label4'},
{v:'5',l:'label5'}]
const filter1 = arrList.filter((a) => a.l !== 'label1' && a.l !== 'label3')
console.log(filter1);
答案 4 :(得分:0)
const filter3 = arrList.filter((a)=> a.l!=='label1'&& a.l!=='label3')
答案 5 :(得分:0)
您可以像这样过滤它。
public static double paintRequired(double totalSquareFeet){
double paintNeeded = totalSquareFeet / 115;
return paintNeeded;
}
public static double costOfPaint(double paintNeeded, double costOfPaint){
double paintCost = paintNeeded * costOfPaint;
return paintCost;
}
public static void main(String[] args){
double costOfPaint = 0;
int totalSquareFeet = 0;
double paintNeeded = 0;
Scanner getUserInput = new Scanner(System.in);
System.out.println("what is the cost of the paint per gallon");
costOfPaint = getUserInput.nextDouble();
System.out.println("How many rooms do you need painted");
int rooms = getUserInput.nextInt();
for(int i = 1; i <= rooms; i++){
System.out.println("how many square feet are in room:" + i);
int roomSquareFeet = getUserInput.nextInt();
totalSquareFeet = roomSquareFeet + totalSquareFeet;
}
System.out.println("the amount of paint needed:" + paintRequired(totalSquareFeet) + "gallons");
System.out.println("the cost of the paint will be: " + costOfPaint(paintNeeded, costOfPaint));
}