"FED25 Array Deduplication" JavaScript practice and how to judge whether a variable is NaN

topic description

Add a method to remove duplicates for the Array object

Example:

Import: [false, true, undefined, null, NaN, 0, 1, {}, {}, 'a', 'a', NaN]

Output: [false, true, undefined, null, NaN, 0, 1, {}, {}, 'a']

answer

  • Utilizing the properties of objects in Set allows storing unique valuesSet  ​​of any type (be it primitive values ​​or object references).  Both and   can be stored in a Set,  and are treated as the same value (NaN is considered the same, although NaN !== NaN). Then use Array.from() to create the array. NaNundefinedNaN

    Array.from() Array objects can be created in the following ways:

    Pseudo-array objects (  length arbitrary objects with one property and several indexed properties)

    Iterable object (you can get the elements in the object, such as Map and Set, etc.)

Array.prototype.uniq = function () {
    return Array.from(new Set(this))
}
  • Traverse the array, add elements to the new array, and judge whether they have been added when adding. Note: ①NaN is not equal to anything, including itself; ②var objA = {}; var objB = {};  console.log(objA == objB); is also not equal, but the title does not require removal; ③arr[i]= {}; arr. indexOf(arr[i]) == i.

        1. When traversing, check whether the current element can be found in its own element. Since NaN is not equal to itself, it cannot be found, so when arr.indexOf(arr[i]) = -1, it means that NaN is encountered. Since NaN needs to be deduplicated if it is repeated, the flag value is added to determine whether NaN has been added.

Array.prototype.uniq = function () {
    var args = this;
    var len = args.length;
    var result = [];
    var flag = true;
    for (var i = 0; i < len; i++) {
        if (args.indexOf(args[i]) != -1) { //NaN不等于任何,包括他自身,所以args.indexOf(args[i])遇到NaN永远返回-1
            if (i === args.indexOf(args[i])) {
                result.push(args[i]);
            }
        } else if (flag) {
            result.push(args[i]);
            flag = false;
        }
    }
    return result
}

        2. Use JavaScript's built-in method to determine whether element a is NaN. There are two ways to judge NaN:

isNaN(a):isNaN()  If the parameter is not a Number type, it will convert the parameter to Number first, so if it is used directly, it will also return true when a string is passed in, ie  isNaN('str'); // true .

Object.is(a, NaN): can only recognize NaN

Array.prototype.uniq = function () {
    var arr = this;
    var result = [];
    var len = arr.length;
    var flag = true
    for (var i = 0; i < len; i++) {
        var type = typeof arr[i]
        if (flag && Object.is(arr[i], NaN)) {
            result.push(arr[i]);
            flag = false;
        } else if (result.indexOf(arr[i]) == -1 && !Object.is(arr[i], NaN)) {
            result.push(arr[i]);
        }
    }
    return result;
}

Guess you like

Origin blog.csdn.net/qq_42101569/article/details/126525304