首页 > 代码库 > 数组之判断取值
数组之判断取值
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Document</title>
<style>
/*css reset开始*/
body,h1,h2,h3,h4,h5,h6,p,dl,dd,ul,ol,pre,form,input,textarea,th,td,select{margin: 0;padding: 0;font-size: 14px;font-family: "Microsoft Yahei";}
input{width: 300px;height: 20px;vertical-align: middle;}
li{list-style: none;vertical-align: top;}
a{text-decoration: none;}
img{border: none;vertical-align: top;}
table{border-collapse: collapse;}
textarea{resize: none;overflow: auto;}
body{margin-top: 10px;}
/*css reset结束*/
form{width: 300px;}
fieldset{margin: 20px;border: 2px dashed palevioletred;}
</style>
<script>
window.onload = function(){
var arr = [‘100px‘, ‘abc‘-6, [], -98765, 34, -2, 0, ‘300‘, , function(){alert(1);}, null, document, [], true, ‘200px‘-30,‘23.45元‘, 5, Number(‘abc‘), function(){ alert(3); }, ‘xyz‘-90 ];
var oAllnum = document.getElementById(‘allNum‘);
var oNum = document.getElementById(‘Num‘);
var oMaxNum = document.getElementById(‘maxNum‘);
var oNanLocation = document.getElementById(‘nanLocation‘);
var arr1 = [];
var arr2 = [];
var arr3 = [];
// 1.数组里所有的数字:↓↓
for(var i=0;i<arr.length;i++){
if(typeof arr[i] == ‘number‘ && arr[i] === arr[i]){
arr1.push(arr[i]);
}
}
oAllnum.value = http://www.mamicode.com/arr1;
// 2.数组里可以转成数字的:↓↓
for(var i=0;i<arr.length;i++){
if(isNaN(parseInt(arr[i])) == false){
arr2.push(arr[i]);
}
}
oNum.value = http://www.mamicode.com/arr2;
// 3.可以转成数字的再从中取最大值:↓↓
var tmp =arr2;
var max = tmp[0];
for(var i=1;i<tmp.length;i++){
if(max<tmp[i])
{
max=tmp[i];
}
}
oMaxNum.value=http://www.mamicode.com/max;
// 4.NAN所在的位置:↓↓
for(var i=0;i<arr.length;i++){
if(typeof arr[i] == ‘number‘ && isNaN(Number(arr[i]))){
arr3.push(i);
}
}
oNanLocation.value = http://www.mamicode.com/arr3;
}
</script>
</head>
<body>
<form action="">
<fieldset>
<legend>输出结果</legend>
arr数组里所有的数字:<input type="text" id="allNum"/><br/><br/>
arr数组里可以转成数字的: <input type="text" id="Num"/><br/><br/>
把转成数字以后的,再取最大值:<input type="text" id="maxNum"/><br/><br/>
NaN所在的位置:<input type="text" id="nanLocation"/>
</fieldset>
</form>
</body>
</html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Document</title>
<style>
/*css reset开始*/
body,h1,h2,h3,h4,h5,h6,p,dl,dd,ul,ol,pre,form,input,textarea,th,td,select{margin: 0;padding: 0;font-size: 14px;font-family: "Microsoft Yahei";}
input{width: 300px;height: 20px;vertical-align: middle;}
li{list-style: none;vertical-align: top;}
a{text-decoration: none;}
img{border: none;vertical-align: top;}
table{border-collapse: collapse;}
textarea{resize: none;overflow: auto;}
body{margin-top: 10px;}
/*css reset结束*/
form{width: 300px;}
fieldset{margin: 20px;border: 2px dashed palevioletred;}
</style>
<script>
window.onload = function(){
var arr = [‘100px‘, ‘abc‘-6, [], -98765, 34, -2, 0, ‘300‘, , function(){alert(1);}, null, document, [], true, ‘200px‘-30,‘23.45元‘, 5, Number(‘abc‘), function(){ alert(3); }, ‘xyz‘-90 ];
var oAllnum = document.getElementById(‘allNum‘);
var oNum = document.getElementById(‘Num‘);
var oMaxNum = document.getElementById(‘maxNum‘);
var oNanLocation = document.getElementById(‘nanLocation‘);
var arr1 = [];
var arr2 = [];
var arr3 = [];
// 1.数组里所有的数字:↓↓
for(var i=0;i<arr.length;i++){
if(typeof arr[i] == ‘number‘ && arr[i] === arr[i]){
arr1.push(arr[i]);
}
}
oAllnum.value = http://www.mamicode.com/arr1;
// 2.数组里可以转成数字的:↓↓
for(var i=0;i<arr.length;i++){
if(isNaN(parseInt(arr[i])) == false){
arr2.push(arr[i]);
}
}
oNum.value = http://www.mamicode.com/arr2;
// 3.可以转成数字的再从中取最大值:↓↓
var tmp =arr2;
var max = tmp[0];
for(var i=1;i<tmp.length;i++){
if(max<tmp[i])
{
max=tmp[i];
}
}
oMaxNum.value=http://www.mamicode.com/max;
// 4.NAN所在的位置:↓↓
for(var i=0;i<arr.length;i++){
if(typeof arr[i] == ‘number‘ && isNaN(Number(arr[i]))){
arr3.push(i);
}
}
oNanLocation.value = http://www.mamicode.com/arr3;
}
</script>
</head>
<body>
<form action="">
<fieldset>
<legend>输出结果</legend>
arr数组里所有的数字:<input type="text" id="allNum"/><br/><br/>
arr数组里可以转成数字的: <input type="text" id="Num"/><br/><br/>
把转成数字以后的,再取最大值:<input type="text" id="maxNum"/><br/><br/>
NaN所在的位置:<input type="text" id="nanLocation"/>
</fieldset>
</form>
</body>
</html>
数组之判断取值
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。