undefinedfix
Sign in

JS is an array with several empty items. output

Tedet edited in Mon, 02 May 2022
arr.forEach((item, idx) => {
        if (item == '') {
          switch (idx) {
            case 0:
              console.log("第一种情况")
              break;
            case 1:
              console.log("第二种情况")              
              break;
            case 2:
              console.log("第三种情况")              
              break;
            case 3:
              console.log("第四种情况")              
              break;
            case 4:
              console.log("第五种情况")              
              break;
          }
        }
        
      })

Now the output is to print out all the empty items. The result I hope to get is: even if the five items entered are all empty, only one result will be printed out. Any one will do. How to realize it?

4 Replies
newbee
commented on Mon, 02 May 2022
var i = arr.findIndex(item => item == '')

Then judge by I

NataliaPC
commented on Tue, 03 May 2022

const status = [
  '第0种情况',
  '第1种情况',
  '第2种情况',
  '第3种情况',
  '第4种情况',
  '第5种情况',
  '第6种情况',
]
arr.forEach((item, idx) => {
  if (item == '') {
    console.log(status[idx])
  }
})

So?

Chuck
commented on Tue, 03 May 2022
let arr = ['', '', '']
let flag = false
arr.forEach((item, idx) => {
    if (item == '') {
        flag = true
    }
})
if (flag) {
    console.log('空');
}
Joshua
commented on Tue, 03 May 2022

arr.find ((item, index) => { console.log ("the first" + (index + 1) + "case"); return item = = ';});

lock This question has been locked and the reply function has been disabled.