【问题标题】:I can not understand this code我无法理解这段代码
【发布时间】:2018-04-04 08:29:37
【问题描述】:

我不得不删除数组中的相同数据。
我发现这段代码和它的工作方式完全符合我的要求,但我无法理解这段代码的一部分。
请解释这段代码和这是什么>>> a[this[i]]

Array.prototype.unique = function() {
    var a = {}; //new Object
    for (var i = 0; i < this.length; i++) {
        if (typeof a[this[i]] == 'undefined') {
            a[this[i]] = 1;
        }
    }
    this.length = 0; //clear the array
    for (var i in a) {
        this[this.length] = i;
    }
    return this;
  };

【问题讨论】:

  • 该代码已过时且不太可靠。 (当用于字符串数组时,它将删除'toString''hasOwnProperty' 等,将任何其他类型转换为字符串,并且语言规范允许产生乱序结果。)使用Array.from(new Set(array))得到一个没有重复的数组。
  • i 是一个数字,this[i] 是这个数组的一个元素,a[this[i]]var a 中的一个属性
  • @Ry - 这将如何与 Internet Exploder 一起工作:p - 而且,这不会“就地”改变数组 - 也许这也很重要
  • @Ry,它根本不会删除这些方法......不知道你为什么认为它会
  • @JaromandaX:你试过了吗? ['toString', 'hasOwnProperty', 'foo'].unique()

标签: javascript arrays typeof


【解决方案1】:

请查看解释该行代码的每一行之前的 cmets //向数组原型添加唯一函数,以便所有数组都可以具有唯一的 //函数访问

    Array.prototype.unique = function() {
        //creating a temp object which will hold array values as keys and 
         //value as "1"  to mark that key exists
        var a = {}; //new Object
        //this points to the array on which you have called unique function so
        //if arr = [1,2,3,4] and you call arr.unique() "this" will point to 
        //arr in below code iterating over each item in array
        for (var i = 0; i < this.length; i++) {
        //idea is to take the value from array and add that as key in a so 
        //that next time it is defined. this[i] points to the array item at 
        //that index(value of i) //and a[this[i]] is adding a property on a 
        //with name "this[i]" which is the value //at that index  so if value 
        //at that index is lets say 2 then a[this[i]] is //referring to 
        //a["2"].thus if 2 exists again at next index you do not add it to a
        //again as it is defined
            if (typeof a[this[i]] == 'undefined') {
                a[this[i]] = 1;
            }
        }
        this.length = 0; //clear the array
        //now in a the properties are unique array items you are just looping 
        //over those props and adding it into current array(this) in which 
        //length will increase every //time you put a value 
        for (var i in a) {
            this[this.length] = i;
        }
        //at the end returning this which is the modified array
        return this;
      };

//编辑

a[this[i]] 的存储值为 1,对于 a 中的所有键,它将为 1。 你从

开始
arr = [1,2,3,2,3,4];

当你调用 arr.unique 第一个循环中的代码创建了一个类似这样的东西

a = {
"1":1,
"2":1,
"3":1,
"4":1
}

因此您可以看到只有唯一值作为 a 中的属性。 现在在 for-in 循环中,您只需获取 a(即 1,2,3,4)的键并将其添加到数组(this)中。

希望这有助于让我知道您是否需要更多详细信息

【讨论】:

  • 注意:if (typeof a[this[i]] == 'undefined') { 是多余的……只要a[this[i]] = 1; 就可以了
  • 感谢它的帮助很大,但我仍然混淆 a[this[i]] =1; 它的存储价值如何不是“1”。我认为a[] 的所有值都必须是'1'
  • @JHYOO:我不知道你是否看到了这个答案的编辑,但它解释了这一点。是的,a 的每个值都是1,但重要的是键。
  • @Ry︁:我看到了编辑后的答案。现在我完全明白了;)
【解决方案2】:

a[this[i]] =>
this[i] -> 从当前对象中获取 i 元素,在本例中为数组。
a[] -> 从 var a 中指定的位置获取元素 @ 987654326@,在这种情况下this[i]里面的值是什么

【讨论】:

    猜你喜欢
    • 2018-02-21
    • 1970-01-01
    • 2012-10-24
    • 1970-01-01
    • 2021-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-04
    相关资源
    最近更新 更多