一道javascript數組操作題

來源:互聯網
上載者:User

標籤:dex   round   nbsp   typeof   console   amp   int   []   資料   

題目如下:

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 ];//1、找到arr裡所有的數字:-98765, 34, -2, 0, 5//2、找到可以轉換成數位:"100px", -98765, 34, -2, "300", "23.45元", 5//3、把轉換成數字以後,最大值判斷出來:300//4、把NaN所在的位置找出來:1, 14, 17, 19

 

 

1、找到arr裡所有的數字:-98765, 34, -2, 0, 5

//1、找到arr裡所有的數字:-98765, 34, -2, 0, 5var num1 =[];for(var i = 0 ;i<arr.length; i++){    //篩選出數字和能轉換成數位字串    var re = /^(\-?)\d+$/;      if(re.test(arr[i]) && typeof(arr[i]) != ‘string‘){        num1.push(arr[i]);    }    }console.log(num1);

 

 

2、找到可以轉換成數位:"100px", -98765, 34, -2, "300", "23.45元", 5

//2、找到可以轉換成數位:"100px", -98765, 34, -2, "300", "23.45元", 5var num2=[];for(var j =0; j<arr.length; j++){    //parseInt解析一個字串,並返回一個整數    if(parseInt(arr[j])){        num2.push(arr[j]);    }}console.log(num2);

 

3、把轉換成數字以後,最大值判斷出來:300

//3、把轉換成數字以後,最大值判斷出來:300//假設最大值為數組的第一個元素,迴圈比對var _max = num2[0];for(var m=0; m<num2.length; m++){    if(_max < num2[m] ){        _max = num2[m];    }    }console.log(_max);

 

4、把NaN所在的位置找出來:1, 14, 17, 19

//4、把NaN所在的位置找出來:1, 14, 17, 19var _index = [];for(var n=0; n<arr.length; n++){    if(Number.isNaN(arr[n])){        _index.push(n);    }}console.log(_index);

 

查了資料才完成的,不用忘得好快啊

 

有感:“溫故而知新”,已記之~

一道javascript數組操作題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.