What is the advantage of using a foreach loop to overwrite values instead of returning all values?

Here is an array that needs to be stored in local storage when the DOM is created.

this.headers.push( 
    {
       text: "Name",
       align: "center",
       sortable: true,
       value: "name",
       align: "start",
     },
     {
       text: "Company",
       align: "center",
       sortable: true,
       value: "company",
       align: "start",
     },
     {
       text: "Phone",
       align: "center",
       sortable: true,
       value: "phone",
       align: "start",
     }
   )

The value of each object should be stored in local storage under the key name el_columns. I have attempted it like this:

this.headersList.forEach((element) => {
      localStorage.setItem(
        "el_columns",
        JSON.stringify(element.value)
      );
    });

The code above works but only stores one value, which ends up being the last object's value - phone. The desired output should be something like ['name', 'company', 'phone'].

Answer №1

Here's a suggestion

 Go through each item in the headers list and store it in local storage as a JSON string for future reference.
      this.headersList.forEach((element) => {
        localStorage.setItem(
          "el_columns_"+element.text,
          JSON.stringify(element.value)
        );
      });
    

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

Node.js and Socket.IO struggle with message delivery

My goal was to develop a chat-like application, so I decided to work with nodejs and socket.io. To simplify things and get a better understanding of how it all functions, I created a button that emits a message to the server. My expectation was that this m ...

Exploring Angular: Looping through an Array of Objects

How can I extract and display values from a JSON object in a loop without using the keyValue pipe? Specifically, I am trying to access the "student2" data and display the name associated with it. Any suggestions on how to achieve this? Thank you for any h ...

Issue: Unable to find suitable routes for navigation. URL Segment: 'profile' or Encounter: Server could not load resource as response status is 401 (Unauthorized)

Currently, I am working on the MEANAUTH application and encountering an issue with accessing user profiles using angular jwt. When attempting to log in and access user profiles at https://localhost:3000/profile, I receive the following error message: Faile ...

The mysterious case of the missing CSS file in Node.js

I recently set up a new Node.js Express Project. However, I am facing an issue with the index.ejs file showing the error: Undefined CSS file ('/stylesheets/style.css'). Here is the content of the index.ejs file: <!DOCTYPE html> <html& ...

vue-form and vue-material are not compatible with each other

In my experience, using a Vue form on a regular HTML <input> element allows validation to work as expected. However, when I switch to using the <md-input> element, the validation no longer functions and an error message is displayed: Element ...

What is the best method to retrieve JSON data from a Rest API?

In my JavaScript code, I am creating an object: var t = null; $.getJSON('http://localhost:53227/Home/GetData', function (data) { alert(data); t = data; }); alert(t); After ...

When you utilize the [Authorize] attribute in an ASP.NET MVC controller, it effectively prevents any Script code from executing

I have encountered an issue with my JavaScript code within script tags in my ASP.NET MVC project. Everything runs smoothly, but as soon as I include the Authorize keyword in my controller, the JavaScript stops working. Strangely, I have been unable to find ...

Confirm the attributes of a table column

How can I ensure that a specific column in an HTML table is validated using a click function in JavaScript or jQuery? <table class="table table-bordered table-striped table-responsive" id="tableid"> <thead> <tr> ...

The proxy feature in create-react-app does not function properly

When setting up my React app using create-react-app, I included the following in my package.json: After setting "proxy":"http://localhost:3001" in the package.json file to point to my API server running on port 3001, I encountered an issue where requests ...

Tips for resetting and configuring timer feature?

A feature in my quiz app requires setting up a timer in the controller that counts for 30 seconds and stops the quiz if there is no activity within that time frame. The timer should reset and start counting again if there is any activity. I have implemente ...

Is there a way to prevent continuous repetition of JavaScript animated text?

I'm working on a code that displays letters and words one by one, but I can't figure out how to stop it from repeating. Can someone help me with this? <div id="changeText"></div> <script type="text/javascript"> var te ...

Utilizing scroll functionality within a DIV container

I have the following Javascript code that enables infinite scrolling on a webpage. Now, I am looking to implement this feature within a specific DIV element. How can I modify this code to achieve infinite scroll functionality inside a DIV? Any assistance ...

Angular - ui-router states are not being detected

I'm currently working on a Spring and Angular JS web application project. The structure of the project is as follows:https://i.sstatic.net/xgB4o.png app.state.js (function() { 'use strict'; angular .module('ftnApp') .con ...

The focus() function fails to execute properly in Vue for NativeScript when v-for is used

One of the challenges I'm facing involves setting focus on the first textfield when a button is pressed. I found a code snippet in the playground without using v-for and it works perfectly fine. However, as soon as I introduce v-for into the code, e ...

Using jQuery to display a div after a 2-second delay on my website, ensuring it only appears once and does not reappear when the page is refreshed or when navigating to a

I manage a website that includes a blog section. Every time someone visits the site, I want a popup window to appear. (To achieve this, follow these steps - Utilize jQuery for showing a div in 5 seconds) I would like this popup to only be displayed once ...

It is not possible to submit two forms at once with only one button click without relying on JQuery

I need to figure out a way to submit two forms using a single button in next.js without relying on document.getElementById. The approach I've taken involves having two form tags and then capturing their data in two separate objects. My goal is to hav ...

Most effective method for populating a mixed type byte array at present

Is there a simple method to organize different pieces of data into specified byte ranges when sending and receiving a byte stream? So far, I've been able to convert individual primitive datatypes into bytes, but I'm looking for a way to allocate ...

Can you suggest a simpler approach to implementing this function?

Greetings to all who are perusing this message. I have devised a technique for retrieving today's date along with the current time. If the deadline value in the database is null, it will fetch the current datetime and format it correctly. Otherwise, ...

What is the best way to determine the dimensions of a KonvaJs Stage in order to correctly pass them as the height/width parameters for the toImage function

Currently, I am using KonvaJs version 3.2.4 to work with the toImage function of the Stage Class. It seems that by default, toImage() only captures an image of the visible stage area. This is why there is a need to provide starting coordinates, height, and ...

Looking for a way to easily swipe through videos?

My mobile phone viewport displays a series of pictures and videos, with the swipeleft/right function enabled for browsing. However, I noticed that while the swipe feature works fine for images, it stops functioning when a video is displayed. Can anyone p ...