Array of JavaScript study notes (2)

Array of JavaScript study notes (2)

1. ['1','2','3'].map(parseInt)What is the output and why?

['1','2','3'].map(parseInt)//[1,NaN,NaN]

// map has three parameters: array element, element index, array itself
// parseInt has two parameters, element itself and base parseInt(string,radix)
['1','2','3'].map(parseInt); ['1','2','3'].map(function(item,index,array){ return parseInt(item,index); });
parseInt("1",0); => 1
parseInt("2",1); = > NaN
parseInt("3",2); => NaN

Syntax: parseInt(string, radix)

Parameters: radix

Optional. Indicates the radix of the number to parse. The value is between 2 and 36.
If the argument is less than 2 or greater than 36, parseInt() will return NaN.
When the value of the parameter radix is ​​0 or the parameter is not set, parseInt() will judge the radix of the number according to the string. If the parameter string starts with "0x" or "0X", it will be base 16. If string starts with a number from 1 to 9, parseInt() will parse it as a decimal integer.

['2018-03-05', '2013-06-12','2019-03-12','2018-03-05','2014-02-22']2. Deduplicate and sort the array

let arr = [...new Set(['2018-03-05', '2013-06-12','2019-03-12','2018-03-05','2014-02-22'])].sort(function(a,b){
  return a<b ? -1:1; 
})
//["2013-06-12", "2014-02-22", "2018-03-05", "2019-03-12"]

3. Array deduplication

method one

var arr = [1, 1, '', '', 'a', 'a', true, true, 'true', 'true', false, false, 'false', 'false']
function uniqueArray(array) {
    var arr1 = []
    for (let i = 1; i < array.length; i++) {
        if (arr1.indexOf(array[i]) === -1) {
            arr1.push(array[i])
        }
    }
    console.log(arr1)// [1, "", "a", true, "true", false, "false"]
    return arr1
}
uniqueArray(arr)

Method Two

var arr={'0':null,'1':null,'2':'','3':'','4':4,'5':4,length:'6'}
function uniqueArray(array) {
    if (Array.isArray(array)) {
        array1=array
    } else if(array.length>0){
        var array1 = Array.prototype.slice.call(array)
    }else{
        console("参数必须是数组或类数组对象")
        return
    }
    var arr1 = []
    for (let i = 1; i < array1.length; i++) {
        if (arr1.indexOf(array1[i]) === -1) {
            arr1.push(array1[i])
        }
    }
   
    console.log(arr1)//[null, "", 4]
    return arr1
}
uniqueArray(arr)

Method three

var arr = [1, 1, ' ', ' ', 'a', 'a', undefined, undefined, null, null]
function uniqueArrar(array) {
    if (Array.isArray(array) && array.length >1){
        var temp=[]
        array.forEach(function(value,index){
            if(temp.indexOf(value)===-1){
                temp.push(value)
            }
        })  
    }
    return temp 
}
uniqueArrar(arr)//[1, " ", "a", undefined, null]

3. Array [1,2,3,4,5,'6',7,'8','a','b','z']out of order

let tempArr = [1, 5, '6', 7, '8', 'a', 'b', 'z'].sort(function () {
    return Math.random() > 0.5 ? -1 : 1;
})

4. Find [1, 10, 11, -1, 8, 9]the maximum and minimum values

var arr = [1, 10, 11, -1, 8, 9]
function MaxMinPlus(arr) {
    if( Array.isArray(arr)){
        var max=Math.max.apply(null, arr) 
        var min=Math.min.apply(null, arr)
    }
    console.log(max)//11
    console.log(min)//-1
}
MaxMinPlus(arr) 

If it is an array-like, you can use Array.prototype.slice.call()the method to convert to an array

5. An array stores the information of multiple personnel, and the information of each person is composed of name and age, and the age is sorted from young to old;

var obj = [
  {age:4,name:'张三'},{age:3,name:'李四'},{age:5,name:'王五'},{age:1,name:'赵二'}
]

var obj1 = obj.sort(function(a,b){
  return a.age - b.age;
})
console.log(obj1)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325160248&siteId=291194637