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

  • SEARCH
  • Home
  • 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 8645609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:35:38+00:00 2026-06-12T12:35:38+00:00

I’m listing every DOM element’s id on the page with: var listy = $(*[id]).map(function(){

  • 0

I’m listing every DOM element’s id on the page with:

 var listy = $("*[id]").map(function(){
    return this.id;
 }).get().join(',');

So, example output of listy would be:

"home,links,stuff,things"

Now I want to convert the listy Array to an Object, like:

function toObject(arr) {
    var rv = {};
    for (var i = 0; i < arr.length; ++i)
        if (arr[i] !== undefined) rv[i] = arr[i];
    return rv;
}

But that wil put everything it in an Object like:

0: "h", 1: "o", 2: "m", etc...

What I obviously want is:

0: "home", 1: "links, 2: "stuff", etc..

Where am I going wrong, I got the toObject() from: Convert Array to Object

Which brings me to a similar, but different question:

Indexing page elements to JSON object? Or jQuery selector it every time?

  • 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-06-12T12:35:39+00:00Added an answer on June 12, 2026 at 12:35 pm

    This is likely the shortest code you can write to get your object:

    // your existing code (a tiny bit changed)
    var idArray = $("*[id]").map(function() {
        return this.id;
    }).get();
    
    // this one liner does the trick
    var obj = $.extend({}, idArray);
    

    A better approach for your problem – associative array

    But as I’ve read in your comments in other answers this object structure may not be best in your case. What you want is to check whether a particular ID already exists. This object

    {
        0: "ID1",
        1: "otherID",
        ...
    }
    

    won’t help too much. A better object would be your associative array object:

    {
        ID1: true,
        otherID: true,
        ...
    }
    

    because that would make it simple to determine particular ID by simply checking using this condition in if statement:

    if (existingIDs[searchedID]) ...
    

    All IDs that don’t exist would fail this condition and all that do exist will return true. But to convert your array of IDs to this object you should use this code:

    // your existing code (a tiny bit changed)
    var idArray = $("*[id]").map(function() {
        return this.id;
    }).get();
    
    // convert to associative "array"
    var existingIDs = {};
    $.each(idArray, function() { existingIDs[this] = true; });
    

    or given the needed results you can optimise this a bit more:

    var existingIDs = {};
    $("*[id]").each(function() { existingIDs[this.id] = true; });
    

    This will speed up your existing ID searching to the max. Checking ID uniqueness likely doesn’t need hierarchical object structure as long as you can get the info about ID existence in O(1). Upper associative array object will give you this super fast possibility. And when you create a new object with a new ID, you can easily add it to existing associative array object by simply doing this:

    existingIDs[newID] = true;
    

    And that’s it. The new ID will get cached along with others in the same associative array object.

    Caution: I can see your code has implied global (listy variable). Beware and avoid if possible.

    Performance testing

    As @Blazemonger suggests this doesn’t hold water I’d say that this claim may be true. It all boils down to:

    • the number of elements with IDs you have
    • number of searches you’d like to do

    If the first one is normally low as in regular HTML pages where CSS classes are more frequently used than IDs and if the second one is large enough then this performance test proves that associative array can be a fair bit faster than using jQuery alone. HTML Used in this test is a copy of Stackoverflow’s list of questions on the mobile site (so I had less work eliminating scripts from the source). I deliberately took an example of a real world site content than prepared test case which can be deliberately manufactured to favour one or the other solution.

    If you’re searching on a much smaller scale, than I using straight forwards jQuery would be faster because it doesn’t require start-up associative array preparation and gets off searching right away.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
This could be a duplicate question, but I have no idea what search terms

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.