var OldArray = {
a,
1,
5,
3,
b,
6,
2
};
现在要获得一个新的NewArray,要求两个:一是为数字;二是大于3.如何得到这个新的NewArray!
------解决方案--------------------------------------------------------
- JScript code
function aa() { var OldArray = []; OldArray[0] = "a"; OldArray[1] = "1"; OldArray[2] = "5"; OldArray[3] = "3"; OldArray[4] = "b"; OldArray[5] = "6"; OldArray[6] = "2"; OldArray[7] = "a"; debugger; var NewArray = []; for (var i = 0; i < OldArray.length; i++) { if (!isNaN(OldArray[i])) { if (Number(OldArray[i]) > 3) { NewArray[Number(NewArray.length)] = OldArray[i]; } } } var str = ""; for (var i = 0; i < NewArray.length; i++) { str += ("-" + NewArray[i]) } alert(str); }
------解决方案--------------------------------------------------------
- JScript code
var OldArray =['a', 1, 5, 3, 'b', 6, 2, 7]; var NewArray = []; var pattern = /\d+/; for (var i in OldArray) { if (pattern.test(OldArray[i])) { if (OldArray[i] >3) { NewArray.push(OldArray[i]); } } }