Skip to content
the poor coder

convert namednodemap to object

Poor Man
Published
Reading time
2 min read
On this page

Converting NamedNodeMap to Object#

As a web developer, you might have worked with DOM (Document Object Model) and encountered the NamedNodeMap object. It is a collection of attributes associated with an element. However, sometimes you may want to convert this NamedNodeMap object into a regular JavaScript object for easier manipulation.

The Problem#

Suppose you have an HTML element with the following attributes:

Text

<div id="example" class="container" data-type="text"></div>

You can fetch the attributes using the attributes property of the element:

Text

let element = document.getElementById("example");
let attributes = element.attributes;

console.log(attributes);
// output: NamedNodeMap {0: id, 1: class, 2: data-type, id: id, class: class, data-type: data-type, length: 3}

As you can see, the attributes property returns a NamedNodeMap object. Now, let's say you want to convert this object into a regular JavaScript object with key-value pairs for each attribute. How can you achieve this?

The Solution#

One way to convert the NamedNodeMap object into a JavaScript object is by iterating over each attribute and adding it as a property to a new object:

Text

let element = document.getElementById("example");
let attributes = element.attributes;
let obj = {};

for (let i = 0; i < attributes.length; i++) {
  let attribute = attributes[i];
  obj[attribute.name] = attribute.value;
}

console.log(obj);
// output: {id: "example", class: "container", "data-type": "text"}

In the above code, we create a new empty object obj. Then, we loop through each attribute in the NamedNodeMap object using a for loop. For each attribute, we extract its name and value using the name and value properties, respectively. Finally, we add a new property to the obj object with the attribute name as the key and the attribute value as the value.

Alternative Solution using Spread Operator#

An alternative way to convert a NamedNodeMap object into a JavaScript object is by using the spread operator (...). We can spread the NamedNodeMap object into an empty object, which will create key-value pairs for each attribute:

Text

let element = document.getElementById("example");
let attributes = element.attributes;
let obj = {...attributes};

console.log(obj);
// output: {0: id, 1: class, 2: data-type, id: id, class: class, data-type: data-type, length: 3}

In the above code, we create a new empty object obj. Then, we use the spread operator (...) to spread the NamedNodeMap object attributes into obj. This creates a new object with key-value pairs for each attribute in attributes.

Conclusion#

Converting a NamedNodeMap object into a JavaScript object can be useful when working with DOM elements in JavaScript. The above solutions provide two ways to achieve this conversion: iterating over each attribute and adding them to a new object, or spreading the NamedNodeMap object into a new object using the spread operator.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC