js commonly used extension methods

In the daily development process, often encounter situations javaScript native object method is not used, so often have javaScript native method for expansion. Here is when the real work, some methods frequently used to do some recording, there is a need can take it.

1, String articles

1.1, strings do add operation:

Precision floating-point problem is an obstacle to javaScript calculated as if some decimal digits in binary representation is infinite. 1.1 For example, the program can not be a true representation 1.1, can only achieve a certain degree of accuracy, but can not avoid the loss of precision.

String.prototype.add = function (arg2) {
    let r1, r2, m
    try {
        r1 = this.toString().split('.')[1].length
    } catch (e) {
        r1 = 0
    }
    try {
        r2 = arg2.toString().split('.')[1].length
    } catch (e) {
        r2 = 0
    }

    m = Math.pow(10, Math.max(r1, r2))
    return (Math.round(this * m + arg2 * m) / m).toString()
}

1.2, strings do subtraction operation

String.prototype.reduce = function (arg2) {
    let r1, r2, m
    try {
        r1 = this.toString().split('.')[1].length
    } catch (e) {
        r1 = 0
    }
    try {
        r2 = arg2.toString().split('.')[1].length
    } catch (e) {
        r2 = 0
    }
    m = Math.pow(10, Math.max(r1, r2))
    return (Math.round(this * m - arg2 * m) / m).toString()
}

2, Number papers

2.1, figure is addition operation

Number.prototype.add = function (arg2) {
    let r1, r2, m
    try {
        r1 = this.toString().split('.')[1].length
    } catch (e) {
        r1 = 0
    }
    try {
        r2 = arg2.toString().split('.')[1].length
    } catch (e) {
        r2 = 0
    }

    m = Math.pow(10, Math.max(r1, r2))
    return Math.round(this * m + arg2 * m) / m
}

2.2, digital subtraction operation do

Number.prototype.reduce = function (arg2) {
    let r1, r2, m
    try {
        r1 = this.toString().split('.')[1].length
    } catch (e) {
        r1 = 0
    }
    try {
        r2 = arg2.toString().split('.')[1].length
    } catch (e) {
        r2 = 0
    }
    m = Math.pow(10, Math.max(r1, r2))
    return Math.round(this * m - arg2 * m) / m
}

3, Array Array articles

3.1, to obtain the maximum array

Array.prototype.getMax = function () {
    return Math.max.apply(null, this.map((seg) => {return +seg}))
}

3.2, obtaining a deep copy of the array of values

Array.prototype.getCopy = function () {
    let that = this
    return JSON.parse(JSON.stringify(that))
}

3.3, replace the array using special characters (symbol) of 0

Array.prototype.replaceZero = function (symbol = '-') {
    return this.map(function (seg) {
        return +seg == 0 ? symbol : seg
    })
}

3.4, obtaining the array element idx specified, wherein if the length of the array length exceeds idx, idx element number is searched and the subtraction length * n

Array.prototype.getItemByIdx = function (idx = 0) {
    function getArrByIdx (list, idx) {
        if (list.length - 1 < idx) {
            idx -= list.length
            return getArrByIdx(list, idx)
        } else {
            return list[idx]
        }
    }
    return getArrByIdx(this, idx)
}

3.5, obtaining a new array element of the array defined by the random number from the composition. count as the required number

Array.prototype.getRandomArrayElements = function (count) {
    var shuffled = this.slice(0),
        i = this.length,
        min = i - count,
        temp, index
    while (i-- > min) {
        index = Math.floor((i + 1) * Math.random())
        temp = shuffled[index]
        shuffled[index] = shuffled[i]
        shuffled[i] = temp
    }
    return shuffled.slice(min)
}

3.6, get a random element in the array

Array.prototype.getRandomElement = function () {
    let lth = this.length - 0.1
    return this[Math.floor(Math.random() * lth)]
}

3.7, the array elements and seek plus

Array.prototype.sum = function () {
    return eval(this.join('+'))
}

Guess you like

Origin www.cnblogs.com/JHCan333/p/11698235.html