Storing the array in the browser's local storage

Here is the complete code I am working on

My goal is to save the visited array to local storage. Then, I want to use an if statement to check if the array has been stored. If it has, the code will execute:

return;

This will end the code execution and make the button non-functional. For example:

if (store.length == 3) {
    document.getElementById('btn').className = 'maxques';
    alert('You have completed this category');
    console.log(store);
    return; }

I am struggling with finding a way to properly store the array. I have tried using JSON.stringify followed by JSON.parse but without success. Any suggestions or ideas?

Answer №1

To avoid redundancy like @Tom Hart mentioned, consider utilizing both the localstorage.setItem and localStorage.getItem functions. These functions require parameters in the form of key, value, and key, respectively.

Answer №2

When saving your array:

localStorage.savedArray=array.join(":"); //or any other separator

To verify if it has been saved:

if(localStorage.savedArray){
    //The array has been successfully saved!
}

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

I am not getting any output from my Array code. What am I doing wrong or what am I

I was given an assignment in my java basics class to practice using Arrays. The task is to ask users for 10 integers and then output the even numbers, odd numbers, and the sum of all numbers. However, my code seems to only display zeros. I'm not sure ...

Ways to fix: The toString() method in the Object type cannot be used with the arguments (InputStream)

I am currently attempting to access and read a JSON file stored on my personal computer. @Path("/") public class JsonParsing { File f = new File("file.json"); if (f.exists()){ InputStream is = new FileInputStream("file.json"); Str ...

Tips for designing a unique CSS ellipse effect for text styling

Hey there! I have a list of titles in a drop-down menu as strings, for example: "Bharat Untitled offer #564", "Bharat Untitled offer #563" The titles are quite long, so we want to display the first eight characters, followed by '...' and then ...

A guide on developing a React component using dot notation and efficiently sharing state with its child components

In my development project, I am creating a component named NavigationBar and I want to have both mobile and desktop versions of it. My plan is to achieve this by creating separate components using the dot notation. class NavigationBar extends React.PureCo ...

Ways to retrieve the JSON key value pair in a customized format

I have a JSON array structured like this: var region= [{"af":"Africa"},{"as":"Asia"},{"au":"Australia"}] Within the framework I am using, the values of the above array are accessed in the following way: {for r in regions} <option value="${r}" &g ...

Error: Unexpected token : encountered in jQuery ajax call

When creating a web page that requests remote data (json), using jQuery.ajax works well within the same domain. However, if the request is made from a different domain (e.g. localhost), browsers block it and display: No 'Access-Control-Allow-Or ...

Unable to activate the date range picker

I am having trouble with integrating the daterange picker on my webpage. I can't seem to get it to work properly. Can anyone help me figure out what I might be doing wrong or if there's something missing? CSHTML: <div class="has-feedback" &g ...

Working with Jquery to access and iterate through associative objects

I'm encountering an issue with accessing a JSON encoded PHP associative array. The array is retrieved using $.get(); and stored in the data variable. $.get("includes/ajax/public.php", { do: "get-data", id: value },function(data) { // NO DATA if (!dat ...

How to Identify an Array in JavaScript using a String

I'm experiencing some challenges with retrieving a pre-declared array based on user input. This task is designed to help me grasp how to capture user feedback using an HTML drop-down and then return a value using a script. What I aim for in the secon ...

Validating Dropdowns in React: An Essential Guide

I am in the process of developing a React-based application and incorporating Yup for validation. However, I am encountering a Cyclic Dependency error during the validation process. Description: Users will be presented with two dropdown lists of colors, v ...

Array becomes blank once data is submitted by Angular controller

My application is built using a combination of CodeIgniter and Angular. I have encountered an issue where, when posting data from the Angular controller to the CodeIgniter controller, the array appears to be empty (resulting in "array()" when using print_r ...

Randomly generated numerical value in input field

How can I generate a 15-digit number using JS code and then reduce it to just 5 or 6 digits in a text box? <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script type="text/javascript ...

Tips for applying various CSS properties to a single element using styled-components

Is there a way to style similar elements differently using different CSS properties in styled components? For example: <div></div> <div></div> With the knowledge I have, I can do: export const StyledDiv = styled.div` color: red; ` ...

What can be done to improve the elegance of this jQuery function?

I am facing an issue on my webpage where I have a drop-down select box with a default value of (empty). The problem arises when the user selects an entry, and the appropriate form for that entry type should be displayed. However, there are a couple of iss ...

How can one utilize JSON in Express JS?

I am completely new to express js. I recently wrote a simple program to send a JSON request through Postman and receive a response. However, I'm encountering an issue where I am unable to get any response. Every time I try, it just says "could not ge ...

In PHP, determining whether two arrays hold the same values but in a different order

Is there a way to quickly verify in PHP if two arrays contain the same items, even if their order is different? If you are working with integer arrays, you can refer to this solution: How to check if two indexed arrays have same values even if the order i ...

Updating partials with Ajax calls and displaying the updated content in a Rails application

Instead of using erb, I am using haml. So my code looks something like this: $("<%= escape_javascript(render @user) %>").appendTo("#users"); Unfortunately, the code mentioned in is not working for me. Any suggestions or ideas on how to make it w ...

What is the best way to save the output of a Node.js script into a MongoDB

I am looking to create a Node.js script that can ping a host and store the results in MongoDB. Currently, I am using Agenda to schedule the pings at specific times. However, I am having trouble figuring out how to save the execution results to the databa ...

Ways to determine if an element is at the top of a page

I need help with a test case that involves checking if the title scrolls up to the top of the page when a button is clicked. The challenge is that there is a default header on the page, so the title should scroll below that. How can we verify this scenar ...

The presence of parentheses in a JQuery selector

My database contains divs with ids that sometimes have parentheses in them. This is causing issues with my JQuery selector. How can I resolve this problem? Let me provide an example to illustrate: https://jsfiddle.net/2uL7s3ts/1/ var element = 'hel ...