DEV Community

Cover image for DOM is easy: Just make it work like an Array 👍
𒎏Wii 🏳️‍⚧️
𒎏Wii 🏳️‍⚧️

Posted on

4 1 1 2 1

DOM is easy: Just make it work like an Array 👍

Modifying DOM elements without a framework can be tedious. The method names are long, and simple common tasks are split into tiny steps.

Wouldn't it be nice if the DOM was a bit more like an Array? Just push a new element, call sort or even reduce all the values into one.


As part of a larger project, which I'm writing a longer post about at the moment, I built this convenient little helper that works well enough on its own that I want to write a separate little post about it.

domLense is a module of less than 100 lines that lets you pretend a DOM element is actually an array. You can index it, push and pop values, loop it with forEach and even map, reduce, etc. it to get new values.

The way it works is simple: You tell the module how to get the interesting data from an element, and how to deposit different data back to it. Optionally, you define how new elements should be created.

const transforms = {
   get(li) { return li.innerText },
   set(li, text) { li.innerText = text },
   new() { return document.createElement("li") }
}
Enter fullscreen mode Exit fullscreen mode

In this case, our "interesting data" is just the text content of a bunch of <li> elements.

All that's left now is to call domLense on an unsuspecting list element, and we'll be able to treat it just like an array:

const listElement = document.createElement("ol")
const list = domLense(listElement, transforms)

list.push("First item on the list")
list.push("Another item on the list")
list.reverse()
console.log(list.map(text => `* ${text}`).join("\n"))
Enter fullscreen mode Exit fullscreen mode

I've prepared a small codepen demo to play around with this.

The way this works isn't really that complicated. The helper simply creates a proxy that redirects integer indices to the element's child list applying the provided transformations. For any other keys, it tries looking them up in Array.prototype.

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (2)

Collapse
 
greenteaisgreat profile image
Nathan G Bornstein • Edited

Yooo, that's really cool! One of my teachers once said that "all the DOM is, is just a big ass object" and it's stuck with me since. Array manipulation comes to me so much easier than object manipulation does. Even though everything in JS is literally an object, but pppbbbttttt

👏👏👏

Collapse
 
__masashi__ profile image
Masashi

It's actually a pretty good addition to the projects using VanillaJS...
If I happen to work on any major project using Vanilla, I surely will use this.

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay