JavaScript Array not displaying correctly as an array

Consider the following array:

var data = {"result":"success","ids":["00000","54321","123","22222","11111","55555","33333","abc123","123abc","12345","44444"]}
localStorage.ids = data.ids;

However, when attempting to iterate through it using AngularJS:

angular.forEach(localStorage.ids, function(id, key) {
    console.log(id);
});

The output is unexpected:

0
0
0
0
0
,
5
4
3   

If we

console.log(JSON.stringify(localStorage.ids));
, we receive:

"00000,54321,123,22222,11111,55555,33333,abc123,123abc,12345,44444"

Can anyone shed some light on this behavior?

Answer №1

LocalStorage can only store data in string format.

localStorage.setItem('userIDs', JSON.stringify(data.userIDs)); // save the user IDs

var userIDs = JSON.parse(localStorage.getItem('userIDs')); // retrieve the user IDs

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

Tips for Retrieving Array Values into Individual Variables

When working with AJAX and receiving a response in JSON format, I am unsure of how to separate each array format into individual variables. Here is the response: final_string = [{"stars":1,"q1":0,"q2":0,"q3":0,"q4":0,"q5":0,"q6":0,"q7":0,"q8":0,"q9":0 ...

Storing JavaScript object in a database using a PHP script

My current scenario involves having a JavaScript object which can be converted into JSON format using the stringify method. Here's how it looks: var jsObject ={'elem1':'val1', 'elem2': {'elem21':'val1&apos ...

The issue with the Sails.js application is that it fails to update files from assets after

Currently, I am working on a Sails.JS application with Angular.JS as its front-end framework. All the angular files are stored in /assets/linker and they are correctly injected upon start. However, I have encountered an issue where any changes made to the ...

Extracting information from a URL

How can I check if a specific anchor, #gbar, is present in a URL like ? If the anchor is present, I need to hide certain divs and show others. Since this anchor is not part of the query string, I cannot use request.querystring.get(). Any ideas on how to ...

Resolving the ENOTFOUND error in Imgur API call: 443

I keep getting the same error message every time I execute the node.js code below. As someone who is new to working with Authorization Headers in node.js, I must be overlooking something. Can someone offer assistance or direct me to reliable documentatio ...

In relation to the Uncaught Error: Syntax error, an unrecognized expression has been encountered

Recently, I started working on an AngularJS and Node.js application. It's all new to me. In the HTML page, I defined a link as <li><a data-toggle="modal" data-target="#myModal" href="/#/login">Login</a></li>, and then set up th ...

Safeguarding intellectual property rights

I have some legally protected data in my database and I've noticed that Google Books has a system in place to prevent copying and printing of content. For example, if you try to print a book from this link, it won't appear: How can I protect my ...

Disabling multiple textboxes in an array when any one of them has a value entered

Is there a way to automatically disable all text boxes if any one of them has a value? I have an array of cost types and their associated costs. If a cost is entered for any type, all other text boxes for cost types should be disabled. If no cost is ente ...

Managing JavaScript with Scrapy

Spider for reference: import scrapy from scrapy.spiders import Spider from scrapy.selector import Selector from script.items import ScriptItem class RunSpider(scrapy.Spider): name = "run" allowed_domains = ["stopitrightnow.com"] start_urls = ...

Using three.js to render two transparent spheres that overlap each other and handling the intersection visibility

I am struggling with controlling the display of two transparent, overlapping spheres in a webgl context. The issue arises when I want one sphere to be hidden behind the other during overlap. This problem can be observed on this page: Specifically, I want ...

Is there a way to remove a registered broadcast event from rootscope in AngularJS?

Here is some code that I have: angular.module('test') .controller('QuestionsStatusController1', ['$rootScope', '$scope', '$resource', '$state', function ($rootScope, $scope, $resource ...

HTML Multi-Column List Box

Can a List Box be created with List Items displayed in Multiple Columns? I know there are other options available, but I'm curious if this is achievable using the <select> tag ...

Retrieving website content using Angular 2 from Wordpress WP API

I am currently utilizing the Wordpress WP API to extract data for my Angular2 application. To populate my pages, I need to retrieve data and obtain the page SLUG based on the ActivatedRoute. However, I am facing uncertainties on how to accomplish this tas ...

What is the best way to send an ng-click executable action into a directive through a variable?

Using HTML view with a directive: <div click aaa="aaa()" action="action"></div> The controller wants to pass the function bbb() in $scope.action: app.controller('MainCtrl', function($scope) { $scope.aaa = function () { alert(&a ...

What factors contribute to the leakage of JS Event Listeners during ajax jquery calls?

While utilizing this code within a setInterval() function and monitoring performance in Chrome, I noticed a consistent increase in JS Event Listeners (+2 with every call). Is this behavior considered normal? setInterval(function(){ $.ajax({ before ...

AngularJS is encountering an issue with the callback function, resulting in an error

Currently, I am utilizing the $timeout service in Angular to decrease a variable from 100 to 1 in increments of 1/10 seconds. Although I understand that using the $interval service would be a simpler solution, for this particular scenario, I am focused on ...

Modify the directive when the scope variable undergoes changes

Utilizing an http request, I am retrieving data from a json file that I then utilize in my controller. app.controller('mainCtrl', ['$scope', 'loaderService', function ($scope, loaderService) { //Getting data from the s ...

Focusing on a particular table using jQuery

My code is functioning well, but the issue I'm facing is that it is adding rows to every table on the page. How can I specify a particular table in the code? $(document).ready(function() { $.getJSON('/api/getUsers', function(json) { ...

Animating Card depth on hover with Material UI

I am looking to add an animation effect to the Card component when the mouse hovers over it. I'm still fairly new to React and struggling with implementing it. Here is what I've tried so far: <Card linkButton={true} href="/servicios/ ...

controlling the direction in which the pointer is aimed

It might sound like a simple question, but I've been struggling to find a clear answer online. Let's say I have a pointer to an array declared as: static char newMessage[400]; char *p = &newMessage; (I'm not sure if this is correct) Ho ...