A JavaScript array operation problem

Source: Internet
Author: User

The topics are as follows:

var arr = [' 100px ', ' abc ' -6,[],-98765,34,-2,0, ' + ',,functionnulltrue  function() {alert (3);}, ' xyz ' -90 ]; // 1. Find all the numbers in arr: -98765,,-2, 0, 5 // 2, find can be converted into numbers: "100px",-98765, 34,-2, "300", "23.45 Yuan", 5 // 3, the conversion into a number, the maximum value to be judged out: // 4. Find out where Nan is located: 1, +, +

1. Find all the numbers in arr:-98765, 34,-2, 0, 5

// 1. Find all the numbers in arr: -98765,,-2, 0, 5 var num1 =[];  for (var i = 0;i<arr.length; i++) {    // filter out numbers and strings that can be converted to numbers    var re = /^(\-?) \d+$/;       if typeof (Arr[i])! = ' String ') {        num1.push (arr[i]);}    } Console.log (NUM1);

2, find can be converted into numbers: "100px",-98765, 34,-2, "300", "23.45 Yuan", 5

// 2, find can be converted into numbers: "100px",-98765, 34,-2, "300", "23.45 Yuan", 5 var num2=[];  for (var j =0; j<arr.length; J + +) {    //parseint parses a string and returns an integer    if  (parseint (Arr[j])) {        Num2.push (arr[j]);}    } Console.log (num2);

3, the conversion into a number, the maximum value judged out: 300

// 3, the conversion into a number, the maximum value to be judged out: // assuming the maximum value is the first element of the array, the loop ratio var _max = num2[0];  for (var m=0; m<num2.length; m++) {    if(_max < num2[m]) {        =  NUM2[M];    }    } Console.log (_max);

4. Find out where Nan is located: 1, 14, 17, 19

// 4. Find out where Nan is located: 1, +, + var _index = [];  for (var n=0; n<arr.length; n++) {    if(Number.isnan (Arr[n])) {        _index.push (n) ;    }} Console.log (_index);

Check the data to complete, do not forget the fast AH

Thoughts: "Warm and know New", has been remembered ~

A JavaScript array operation problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.