首页 > js 的 for 循环中如何判断值是否是 undefined

js 的 for 循环中如何判断值是否是 undefined

直接上代码:

 
box = ["weixin://resourceid/5d249f3717d4b2416a6427d7000e0ecf", undefined, "weixin://resourceid/5b5a484fb48d4b9fccbd485e0998f6b9"]

function done () {

    for (var i = arr_count(box) - 1; i >= 0; i--) {

      if(typeof(box[i])!='undefined'){

        wx.uploadVoice({

          localId: box[i],

          success: function (res) {
            
            voice.serverId = res.serverId;
           
            server.push(res.serverId)

          }
          
        });
        
      }else{
         server.push('null');
      }
    };
server = ["null","lZhg43ku7vo4blj5Ewm7tXmDX1rOSgujhiVnswUBTZNZTryx_R89FZ122QaAN6T-"]

不知道哪里出了问题。对于 undefined 的判断还尝试过 !box[i] box[i] === undefined ,最后输出的结果总是只有两个.

找到问题了,数组的长度应该是 box.length。

但是又有了另外一个问题,修改后输出的结果是:
<code["null","qU6OU1BlFAA2bGl0eSEKPURyzzEvtMORJPkZGc6KlyPBdh4yLVCIzhwZrwHudsI0","rE-LhO2h6Czo2ARds9iNmLzdRYeQjiWzD7qn845kT2bPLqxXj1aER_HMXXU_53ep"]
不按顺序啊???


 
box = ["weixin://resourceid/5d249f3717d4b2416a6427d7000e0ecf", undefined, "weixin://resourceid/5b5a484fb48d4b9fccbd485e0998f6b9"]

function done () {
    for (var i = arr_count(box) - 1; i >= 0; i--) {
      if (typeof(box[i]) != 'undefined') {
         (function(idx) {
            wx.uploadVoice({
              localId: box[i],
              success: function (res) {
                voice.serverId = res.serverId;
                server[idx] = res.serverId;
              }
            });
         })(i);
      } else {
         server[i] = 'null';
      }
    }
}
【热门文章】
【热门文章】