首页 > 代码库 > 清除数组中的特定数值

清除数组中的特定数值

  题目:清除数组中的undefined

解决方案1:

 var ccc = [1,2,3,undefined,undefined,undefined,undefined,4,5,6]  for(var i = 0;i<ccc.length;i++){  if(typeof ccc[i] == "undefined"){    ccc.splice(i,1)  }}
console.log(ccc)

解决方案2:

var ccc = [1,2,3,undefined,undefined,undefined,undefined,4,5,6];var ddd = [];for(var i = 0;i<ccc.length;i++){  if(typeof ccc[i] !== "undefined"){    ddd.push(ccc[i])  }}
ccc = ddd;console.log(ccc)
var ccc = [1,2,3,undefined,undefined,undefined,undefined,4,5,6]alert(ccc.length)for(var i = ccc.length;i>=0;i--){  if(typeof ccc[i] == "undefined"){    ccc.splice(i,1)  }}
console.log(ccc)

解决方案5:

var ccc = [1,2,3,undefined,undefined,undefined,undefined,4,5,6];ccc.toString().split(/,/)
console.log(ccc)

有木有发现问题哪个解决方案是不对的