Sol*_*olo 4 javascript arrays object
我有一种情况,我有3个不同的数组,其中有很多不同的对象.我已经阅读了很多关于此的问题和博客文章,但我仍然不确定何时使用什么.
PS!我最大的问题是我需要迭代和推送(完美的数组),还要查找是否存在于数组和删除(更适合于对象).不需要特定订单.
我不能允许在两者中都有相同的对象array1,array1clicked
因为它们应该执行不同的操作.
什么时候最好使用对象和我的例子中的数组?我应该用对象替换什么以及应该作为数组保留什么?我很确定它中的物体数量也很重要,对吧?
我目前的代码:
//Objects in arrays are literally custom {objects} with custom prototypes and html
var array1 = [ 20 objects ];
var array1clicked = [];
var array2 = [ 250 objects ];
var array2clicked = [];
var array3 = [ 50 000 objects ];
var array3clicked = [];
//Each object in arrays has event attached
objecthtml.click(function() {
//Add to clicked array
array1clicked.push(thisobject);
//Remove from initial array
var index = array1.indexOf(thisobject);
if (index > -1) {
array1.splice(index, 1);
}
}
//Same with array2 and array3 objects
//Iterations on different conditions
var array1count = array1.length;
var array1clickedcount = array1clicked.length;
//Same with array2 and array3
if(condition1) {
for(a = 0; a < array1count; a++) {
array1[a].div.style.visibility = 'hidden';
}
//Same with array2 and array3 objects
for(a = 0; a < array1clickedcount; a++) {
array1clicked[a].div.style.visibility = 'visible';
}
//Same with array2clicked and array3clicked objects
}
else if(condition2) {
for(a = 0; a < array1count; a++) {
array1[a].div.style.visibility = 'visible';
}
//Same with array2 and array3 objects
for(a = 0; a < array1clickedcount; a++) {
array1clicked[a].div.style.visibility = 'hidden';
}
//Same with array2clicked and array3clicked objects
}
Run Code Online (Sandbox Code Playgroud)
看来你想要一个包含这些操作的数据结构:
对于数组,问题是搜索和删除(使用重建索引)很慢.
对于对象,问题是属性名称只能是字符串.
完美的结构是一套.
var s = new Set();
s.add(123); // insert
s.has(123); // search
s.delete(123); // delete
s.values(); // iterator
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
130 次 |
| 最近记录: |