Exploring Javascript objects and looping through arrays using the hasOwnProperty method

In the code snippet provided, an element is taken from the DOM and stored in a variable called myEl. The second variable, myObj, is an empty object initially. The purpose of this code is to create a new object with attributes based on the properties of the element passed in.

function getAttributes(myEl, myObj){
    attArray = [
        "tagName",
        "id",
        "name"
    ];

    for (var att in attArray){
        if (myEl.hasOwnProperty(attArray[att])) {
                myObj.attributes = {};
                myObj.attributes.hasOwnProperty(attArray[att]) == myEl.hasOwnProperty(attArray[att]);
        };
    };
};

Answer №1

To determine whether an element includes a specified attribute from the provided list, you can utilize .hasAttribute(attributeName) in the following manner:

element.hasAttribute(attributeList[i]) // yields either true or false

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Sharing information in React applications

I'm a beginner when it comes to using javascript and React, so I have a question regarding posting data from within a component. In my scenario, I am utilizing the fetch API for making POST requests. Below is the code snippet I have created: export f ...

The Node.js error message reads: "Cannot set headers after they have been sent" while trying to make a post request

Yes, I understand this issue has been addressed multiple times on stackoverflow, but unfortunately, I haven't found a solution that works for me. The problem arises when trying to make a post request on my nodejs server. The error message states &apo ...

Error: The array index is outside the permissible range

Can someone assist me in understanding the cause of this error? IndexOutOfRangeException: Array index is out of range. (at Assets/Scripts/PlayerCar.js:73) CompareApproximately (det, 1.0F, .005f) UnityEditor.DockArea:OnGUI() Snippet from My code: var Gea ...

Implementing Attributes in JavaScript

Is it possible to dynamically add a src attribute to the source tag within a video tag using JavaScript? I have attempted various methods, such as: $("#source-tag-id").setAttribute('src', 'attribute-value'); document.getElem ...

Streamlining async/await in for loops using Promise.all: A guide

I'm trying to understand how Promise.all() works in this code. I've learned that you can execute async operations concurrently with Promise.all() for better performance. Currently, the code uses nested for-loops (which is not ideal): type ListGro ...

The readyState of Ajax is consistently anything but 4

I have encountered an issue with my JavaScript code. I am running these codes in order to display data based on user input. Despite there being no error connection and the connection happening properly, whenever I enter a name it always goes into the else ...

Using multiple instances of the jQuery datepicker within a datalist

I have successfully implemented the jQuery date picker, but I am encountering an issue where it only works on the first textbox within the datalist. I am struggling to extend the functionality of the datepicker to work on all textboxes within the datalist. ...

How can I create a custom column in Bootstrap-Vue?

Here is my code snippet: <b-table hover :items="usergroupTable.datas" :fields="usergroupTable.fields" :sort-by.sync="sortBy" :sort-desc.sync="sortDesc" ...

Is there a way to stop the automatic triggering of the form submit action?

This form has an action event that triggers when a button is clicked. <form id="prefForm4" enctype="multipart/form-data" method="post" class="masters" action="/Preferences/Preferences/RISSave"> </form> There are two buttons in this form, ...

What is the most efficient method for exporting Express route functions for promise chaining?

Currently, I am in the process of refactoring an API route to utilize ES6 promises instead of callbacks, so as to avoid callback hell. Upon successful conversion to a promise chain, my intention was to extract the .then() functions into a separate file fo ...

Reading from a null array - attempting to retrieve an array from a different activity results in a null value

I have successfully retrieved song names using a cursor and displayed them in a list view in the first activity (MainActivity.java). When I click on a list view item, it redirects to the next activity (NextandPrevious.java) and displays the selected item i ...

Tips for invoking a JavaScript function within an iframe

I'm looking to add an iframe to my HTML document, but I also need to pass a variable from a JavaScript function that is located on the same page (which retrieves cookie values). <script type=text/JavaScript> var generateLinkerUrl = function(url ...

Using JavaScript, create a function that accepts an HTML element as a parameter

I have a script with two functions. One function generates a lengthy HTML string, and the other function processes this string as a parameter. function myFirstFunction() { // Generate HTML content return myHTML; } var myHTML = myFirstFunction(); ...

Troubleshooting a Form Validation Issue with React's useState Hook

I am currently working on form validation for a project. The form includes two essential elements: a textbox for messages and a textbox for recipients. These elements are controlled by state.message (a string) and state.recipients (an array). The state var ...

List of duplicated BLE devices detected in network scanning

Greetings! I am currently working on an Ionic project named BLE Scanner. After facing some challenges, I finally managed to connect to the devices. Below is the code snippet that I discovered online: home.ts (please ignore the DetailPage) import { Compon ...

Issue with displaying ng-repeat data in AngularJS tbody

I am experiencing an issue with using ng-repeat inside a tbody element. Here is the code snippet that is causing trouble: <tbody> <tr ng-repeat="group in groups"> <td>{{ group.name }}</td> </tr> </tbody> Wh ...

How can one effectively develop a component library that is compatible with both React and Preact?

I'm currently in the midst of a project that involves both React and Preact. I've come across a situation where I need to utilize the same component for both React and Preact. Would it be wise to package the component into an npm library? What a ...

The React Context Value keeps coming back as undefined every time

As a beginner working with contexts, I am taking it slow. Recently, I came across logging Providers to test the value and encountered a constant 'undefined' result. To troubleshoot, I tried moving them side by side in the code to see if it makes ...

webpack: the necessity of running TSC before resolving modules manually

Below is my complete webpack.config.js file for reference. If I delete all the *.js files in the app directory, webpack throws the following error: ERROR in ../main.ts Module not found: Error: Can't resolve './app/app.module' in 'C ...

Looping through two arrays with *ngFor in Angular 6 is a breeze

My challenge involves working with two arrays: firstArray= [{id: 1, name:'firstValue1'}, {id:2, name:'firstValue2'}] secondArray= [{ "num": 1, "fullName": SecondValue1 , id:1}] I need to display the data in the following format: fir ...