dri*_*987 3 javascript arrays w2ui
我试图在d3图表之一中实现w2ui 多选,正在进行中.
这是带有问题的示例jsfiddle的链接.
我有三个功能:
//get a column of an array
Array.prototype.getColumn = function(name) {
return this.map(function(el) {
// gets corresponding 'column'
if (el.hasOwnProperty(name)) return el[name];
// removes undefined values
}).filter(function(el) {
return typeof el != 'undefined';
});
};
//remove duplicates in an array
Array.prototype.contains = function(v) {
for (var i = 0; i < this.length; i++) {
if (this[i] === v) return true;
}
return false;
};
Array.prototype.unique = function() {
var arr = [];
for (var i = 0; i < this.length; i++) {
if (!arr.contains(this[i])) {
arr.push(this[i]);
}
}
return arr;
}
Run Code Online (Sandbox Code Playgroud)
我需要在我的一个函数中实现这三个.
问题是,每当我尝试实现这些功能时,Array.prototype我都会在multiselect中获取项目"undefined".数量 "undefined"与具有Array.prototype函数的函数数量直接相关.
如果我删除这些功能,我可以使多选择正常工作(只有多选部分,而不是整个图表.我不明白,是什么导致错误.
任何帮助表示赞赏.谢谢.
通常,当您使用第三方库时,弄乱核心javascript对象是个坏主意.如果您仍希望以这种方式保持并解决此特定问题,请使用Object.defineProperty方法,关闭可枚举位
所以例如改变
Array.prototype.contains = function(v) {
for (var i = 0; i < this.length; i++) {
if (this[i] === v) return true;
}
return false;
};
Run Code Online (Sandbox Code Playgroud)
至
Object.defineProperty(Array.prototype, 'contains', {
enumerable: false,
value: function(v) {
for (var i = 0; i < this.length; i++) {
if (this[i] === v) return true;
}
return false;
}
});
Run Code Online (Sandbox Code Playgroud)
和你添加的其他原型方法类似.
| 归档时间: |
|
| 查看次数: |
579 次 |
| 最近记录: |