我无法理解这段代码
I can not understand this code
我不得不删除数组中的相同数据。
我发现这段代码及其工作方式完全符合我的要求,但我无法理解这段代码的一部分。
请解释这段代码和这是什么 >>> 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;
};
a[this[i]]
=>
this[i]
-> 从当前对象中获取 i
元素,在本例中为 Array.
a[]
-> 在 []
中指定的位置从 var a
获取元素,在这种情况下,this[i]
中的值是什么
请查看解释该行代码的每一行之前的注释
//向数组原型添加唯一函数,以便所有数组都可以具有唯一 //函数访问
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 中的所有键,它将是一个。
你从
开始
arr = [1,2,3,2,3,4];
当你打电话给 arr.unique
第一个循环中的代码创建了一个类似这样的东西
a = {
"1":1,
"2":1,
"3":1,
"4":1
}
因此您可以看到只有唯一值作为 a 中的属性。
现在在 for-in 循环中,您只是获取 a(ie 1,2,3,4) 的键并将其添加到数组 (this).
如果您需要更多详细信息,希望这有助于让我知道
我不得不删除数组中的相同数据。
我发现这段代码及其工作方式完全符合我的要求,但我无法理解这段代码的一部分。
请解释这段代码和这是什么 >>> 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;
};
a[this[i]]
=>
this[i]
-> 从当前对象中获取 i
元素,在本例中为 Array.
a[]
-> 在 []
中指定的位置从 var a
获取元素,在这种情况下,this[i]
请查看解释该行代码的每一行之前的注释 //向数组原型添加唯一函数,以便所有数组都可以具有唯一 //函数访问
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 中的所有键,它将是一个。 你从
开始arr = [1,2,3,2,3,4];
当你打电话给 arr.unique 第一个循环中的代码创建了一个类似这样的东西
a = {
"1":1,
"2":1,
"3":1,
"4":1
}
因此您可以看到只有唯一值作为 a 中的属性。 现在在 for-in 循环中,您只是获取 a(ie 1,2,3,4) 的键并将其添加到数组 (this).
如果您需要更多详细信息,希望这有助于让我知道