Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3217974
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:28:25+00:00 2026-05-17T15:28:25+00:00

I want to store elements as the keys in my array and object as

  • 0

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!!"
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T15:28:26+00:00Added an answer on May 17, 2026 at 3:28 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to loop over the elements of an HTML form, and store the
I want to store the data returned by $_SERVER[REMOTE_ADDR] in PHP into a DB
I want to store values in a bunch of currencies and I'm not too
I want to store a very large amount of vector data on a server
I want to use a QComboBox with the keys and values from a tuple
Is it possible to create a dynamic array using something like this and store
I want to store the current URL in a session variable to reference the
I want to store a large number of sound files in a database, but
I want to store a a c# DateTimeOffset value in a SQL Server 2005
I want to store a large result set from database in memory. Every record

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.