将html-nodes存储为数组中的关键字 [英] storing html-nodes as key in array

查看:83
本文介绍了将html-nodes存储为数组中的关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想将元素存储在我的数组和对象中的键作为值,例如 -

  var arr = []; 
arr [document.getElementById('something')] = {data:'something',fn:function(){}};

但问题是:如果我添加另一个元素,其键值为:$ code >的document.getElementById( 'otherthing')。
稍后将尝试获取以下值: arr [document.getElementById('something')] .data ,我将获得<$ c的值$ c> arr [document.getElementById('otherthing')] 。
例如:

  var arr = []; 
arr [document.getElementById('something')] = {data:'something',fn:function(){}};
arr [document.getElementById('otherthing')] = {data:'otherthing',fn:function(){alert('k'); }};
alert(arr [document.getElementById('otherthing')] .data); // alertotherthing
alert(arr [document.getElementById('something')] .data); //提醒其他,但假设提醒某事

我如何解决这个问题, strong>我可以不保存id,因为我想支持其他节点与no-id _
谢谢Yosy。



<编辑:我的回答,如果你有更好的答案请写下:)(启发卡萨布兰卡的答案)

数组对于id:key-integer,节点id,将其自身的值为
,数组为数组,fn为我的id的键,它将如下所示:

  var idArray = [],nodeArray = []; 

idArray [0] = document.getElementById('hello_ducks');
nodeArray [0] = {data:'hello ducks !!',fn:function(){alert('k'); }};

idArray [1] = document.getElementById('hello');
nodeArray [1] = {data:'hello',fn:function(){}};

var testNode = document.getElementById('hello_ducks'),foundId = -1 / * found id * /;
//我们在我们的数组中有没有testNode?
for(var i = 0; i< idArray.length; i ++){
if(idArray [i] === testNode){
foundId = i;
}
}

//我们找到了我们的元素?
if(foundId> = 0){
alert(nodeArray [foundId] .data); //你好鸭!
}


解决方案


我可以不保存id,因为我想支持其他节点没有id


你应该记住,在为了从数组中获取一个节点,你必须以某种方式识别它。如果节点没有任何这样的唯一标识符,那么如何从数组中检索它,甚至可以首先将其存储起来?



像C ++这样的高级语言,每个对象隐含地都有一个唯一的地址,但是没有这样的东西可以在JavaScript中使用,所以你需要手动提供一些识别对象的方式,而DOM ID是最方便的方法



如果您的某些节点最初没有ID,则最好的方法是简单地分配您自己的唯一ID。






更新:您发布的解决方案将会起作用,但效率并不高,因为您需要每次搜索整个阵列你需要找到一个节点。为什么不简单地将自己的ID分配给没有一个的元素?例如:

  var nextID = 0; 
函数getID(elem){
if(elem.hasAttribute('id')== false || elem.id =='')
elem.id ='dummy-'+ (++节点ID);
return elem.id;
}

这样,您可以随时使用ID作为键:

  var nodeArray = []; 

var e = document.getElementById('hello');
nodeArray [getID(e)] = {...};

var e = / *从某处获取的元素,没有ID * /
nodeArray [getID(e)] = {...}; //仍然工作



I want to store elements as the keys in my array and object as values,for example -

var arr = [];
arr[ document.getElementById('something') ] = { data: 'something' , fn : function(){ } };

But the problem is: If I will add another element with the key of : document.getElementById('otherthing'). And later will try to get the value of : arr[ document.getElementById('something') ].data , I will get the value of arr[ document.getElementById('otherthing') ].
For Example :

var arr = [];
arr[ document.getElementById('something') ] = { data: 'something' , fn : function(){ } };
arr[ document.getElementById('otherthing') ] = { data: 'otherthing' , fn : function(){ alert('k'); } };
alert( arr[ document.getElementById('otherthing') ].data ); // alerts "otherthing"
alert( arr[ document.getElementById('something') ].data ); // alerts "otherthing" but suppose to alert "something"

How I can fix this problem,I can`t save by id,because I want to support other nodes with no-id
Thanks,Yosy.

EDIT:My answer to this,If you have better answer please write it :) (Inspired by casablanca`s answer)
array for id: key-integer the node id, value the node it self
and the array with data and fn with key of my id,It will look like this :

var idArray = [],nodeArray = [];

idArray[0] = document.getElementById('hello_ducks');
nodeArray[0] = { data: 'hello ducks!!' , fn : function(){ alert('k'); } };

idArray[1] = document.getElementById('hello');
nodeArray[1] = { data: 'hello' , fn : function(){ } };

var testNode = document.getElementById('hello_ducks'), foundId = -1 /*found id*/;
// Do we have testNode in our array?
for(var i = 0 ; i < idArray.length; i++ ){
  if( idArray[i] === testNode ){
    foundId = i;
   }
}

// Do we found our element?
if(foundId >= 0) {
    alert( nodeArray[foundId].data ); // "hello ducks!!"
}

解决方案

I can`t save by id,because I want to support other nodes with no-id

You should keep in mind that in order to get back a node from the array, you have to somehow identify it. If a node doesn't have any such unique identifier, then how do you retrieve it later from the array, or even store it in the first place?

In lower-level languages like C++, every object implicitly has a unique address, but there is no such thing you can use in JavaScript, so you need to manually provide some way of identifying an object, and the DOM ID is the most convenient way of doing this.

If some of your nodes don't initially have an ID, the best way to proceed is to simply assign your own unique ID.


Update: The solution you posted will work, but it's not very efficient because you need to search the entire array every time you need to find a node. Why not simply assign your own ID to those elements which don't have one? For example:

var nextID = 0;
function getID(elem) {
  if (elem.hasAttribute('id') == false || elem.id == '')
    elem.id = 'dummy-' + (++nodeID);
  return elem.id;
}

This way you can always use the ID as a key:

var nodeArray = [];

var e = document.getElementById('hello');
nodeArray[getID(e)] = { ... };

var e = /* element obtained from somewhere, doesn't have an ID */
nodeArray[getID(e)] = { ... }; // still works

这篇关于将html-nodes存储为数组中的关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆