我如何用一个if来实现这一目标?第if(fruit != "apple" || fruit != "orange"){章没有达到预期的效果?

fruits.forEach(function(fruit){
  if(fruit != "apple"){ // < ----I want to use a compound OR, or compare multiple strings.
    if(fruit != "orange"){ // <---------I want this nested if gone.
      console.log(fruit + " is not apple or orange [two]");
    }
  }
});

const fruits = ["apple","orange","banana","cherry"];

fruits.forEach(function(fruit){
  if(fruit != "apple"){
    console.log(fruit + " is not apple");
  }
});

fruits.forEach(function(fruit){ //<------- this is what I'm having problems with
  if(fruit != "apple" || fruit != "orange"){
    console.log(fruit + " is not apple or orange [one]");
  }
});

fruits.forEach(function(fruit){
  if(fruit != "apple"){
    if(fruit != "orange"){
      console.log(fruit + " is not apple or orange [two]");
    }
  }
});

推荐答案

@Pointty points指出:

const fruits = ["apple","orange","banana","cherry"];

fruits.forEach(function(fruit){
  if(fruit != "apple"){
    console.log(fruit + " is not apple");
  }
});

fruits.forEach(function(fruit){ //<------- this is what I'm having problems with
  if(fruit != "apple" && fruit != "orange"){ // notice
    console.log(fruit + " is not apple or orange [one]");
  }
});

fruits.forEach(function(fruit){
  if(fruit != "apple"){
    if(fruit != "orange"){
      console.log(fruit + " is not apple or orange [two]");
    }
  }
});

Javascript相关问答推荐

React Hooks中useState的同步问题

将数据从strapi提取到next.js,但响应延迟API URL

使用axios.获取实时服务器时的404响应

提交表格后保留Web表格中的收件箱值?

在grafana情节,避免冲突的情节和传说

我在我的Java代码中遇到了问题,代码的一部分看不到先前定义的对象

如何解决useState错误—setSelect Image不是函数''

覆盖TypeScrip中的Lit-Element子类的属性类型

JQuery Click事件不适用于动态创建的按钮

如何使用TypeScrip设置唯一属性?

Puppeteer上每页的useProxy返回的不是函数/构造函数

基于产品ID更新条带产品图像的JavaScript命中错误

使用线性插值法旋转直线以查看鼠标会导致 skip

我不知道如何纠正这一点.

如何让SVG图标在被点击和访问后改变 colored颜色 ,并在被访问后取消点击时恢复到原来的 colored颜色 ?

P5.js中矩形内的圆弧

我们是否可以在reactjs中创建多个同名的路由

如何处理不带参数的redux thunk payloadCreator回调函数?

递归地将JSON对象的内容上移一级

带元素数组的Mongo聚合