Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

187
Views
How does one synchronize the order of an array of reference-items with the precedence of their related element-nodes within the DOM tree by their IDs?

how to sort element id by knowing their position in DOM

I have one array of element IDs on the page but this array is not sorted by their position in DOM/page. I want to sort them by their position on the page.

Array:

[{ id:'first', type:'TEXT'},{id:'third',type:'DROPDOWN'},{id:'second',type:'TEXT'}]

output:

[{id:'first', type:'TEXT'},{id:'second',type:'TEXT'},{id:'third',type:'DROPDOWN'}]
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

A generic approach which works across any DOM structure follows this steps ...

  1. create a reference-id based lookup-table / map / index for any reference which is listed within the unordered array of reference-items. Use reduce for achieving this task.
  2. retrieve an array of all element-nodes starting at a root-node of ones own choice by using e.g. getElementsByTagName('*').
  3. filter the element-array by existing reference-ids while using the reference-lookup as the filter-function's this context.
  4. map the filtered element array of step 3 back to an array of reference-items by each element's id while using again the reference-lookup as the map-function's this context; thus preserving the order of the former through mapping this order to the final result.

... and might look similar to the next provided example code ...

const unorderedReferenceList = [
  { id: 'bar', type: 'TEXT' },
  { id: 'baz', type: 'DROPDOWN' },
  { id: 'foo', type: 'TEXT' },
];
console.log({ unorderedReferenceList });

const referenceIndex = unorderedReferenceList
  .reduce((index, reference) => {
    index[reference.id] = reference;
    return index;
  }, {});

console.log({ referenceIndex });

const elementList = Array.from(
  document.body.getElementsByTagName('div')
);
console.log({ elementList });

const filteredElementList = elementList
  .filter(function (elm) {
    return (elm.id in this);
  }, referenceIndex);

console.log({ filteredElementList });

const orderedReferenceList = filteredElementList
  .map(function (elm) {
    return this[elm.id];
  }, referenceIndex);

console.log({ orderedReferenceList });
.as-console-wrapper { min-height: 100%!important; top: 0; }
<div>
  <div>

    <div>
      <div>
        <div></div>
        <div id="foo">foo</div>
      </div>
      <div id="bar">bar</div>
    </div>
    
  </div>
  <div>

    <div>
      <div id="baz">baz</div>
      <div></div>
    </div>

  </div>
</div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!