JSON loop malfunctioning

Having trouble looping through a JavaScript object from localStorage. Here is my output:

 {"widget": {"title": "blablabla", "color": "yellow"},"widget": {"title": "lorem ipsum", "color": "black"},......}

// Attempted solution (the key works)

var list = JSON.parse(the localStoragekey);

for(var key in list){
if (list.hasOwnProperty(key)){  
       console.log(list[key])
    }
}

Despite researching online and on sites like Stackoverflow, I have yet to find a functional solution.

Answer №1

Your object contains duplicate keys. Consider using an array instead.

var widgets = [
    {"title": "blablabla", "color": "yellow"},
    {"title": "lorem ipsum", "color": "black"}
]

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

Displaying an alert when the text box contains a duplicate value

I have a set of input fields with various values that can be edited but must not be left empty (if left empty, display an alert). When I update a field with a new value, if that new value matches any of the other input field values, I want to display an al ...

Error in Rails due to a Javascript issue

My journey with learning Javascript started by following an easy game tutorial on RoR. I encountered an error in index.html.erb file which led me to a helpful video tutorial here. <script> var ctx, canvas; var data; window.onload = fun ...

Troubleshooting: Resolving the issue of LABjs not being recognized as a function when used with jQuery scripts

I am currently working on optimizing the load time of my website by implementing LABjs. However, I have encountered an issue with 'Undefined not a function' in relation to jQuery.cookie('mycookie') within my own jQuery script. I would ...

Unable to achieve desired outcome when utilizing ES6 template literals in mailto: string

In my Typescript-driven Angular 2 application, I am creating an email with some pre-filled information that will open the user's default email client. Currently, I have it working by constructing the mailto string directly in the component like this: ...

Using PHP to insert an array into a MySQL database by encoding it into JSON

Requesting assistance with database insertion. //Initiate session session_start(); //Establish database connection require_once 'connection.php'; $data = json_decode(json_encode($_POST), true); foreach($data as $key => $value) { $pref ...

Error: The variable "deleted1" is not declared and cannot be used on the HTML button element's onclick

Hello, I need assistance from someone. I encountered an error message after trying to delete a row even though I have declared the button already. Error: deleted1 is not defined at HTMLButtonElement.onclick Could it be due to the script type being modul ...

Capturing JSON data into a field

I have recently started using scrapy to extract JSON data, but I've encountered an issue where the description includes both the amount and its type (e.g. g, gr, kg, L). Is there a way to separate this information and store it in additional fields? I ...

creation of objects using constructors in Node.js

As I delve into the world of Node.js, a burning question has arisen - how can I make a function accept multiple strings in the form of an array? Picture this scenario: export default (config: Config) => { return { target: 'https://google.com ...

What is the method for determining the total number of hours?

Having trouble calculating total hours based on "employeeId"? Need help with the logic to solve this problem efficiently. Expected Result, [ { "employeeId": "105", "totalHours": "2:45" }, { "employeeId": "777", ...

Moving the camera in Three.js to focus on a specific point

Currently, I am in the process of developing a website utilizing three.js technology. For a preview, you can check out the demo by visiting: . The website is designed on canvas to ensure compatibility with iOS and Android devices. My main query pertains t ...

Utilizing a mouse-over script for image popup alignment

I recently came across the mouse over script below on a website, and I must say it works really well and is very easy to use. The only issue I have with it is the placement of the image. Currently, the image follows the cursor as it moves. Question: Is th ...

Exploring methods to retrieve keys and values from a JSON array and nested dictionaries in PostgreSQL

I'm dealing with a nested structure JSON and I need to extract specific elements (keys) and their corresponding values. How can I access "sky: selling": "1" or "U1": "0000"? I've tried using json_object_keys and json_array_elements to extract the ...

Steps for generating an observable that mirrors the structure of an observable array

I am in the process of creating an observable array using JSON data retrieved from the server. var ViewModel = function (data) { var self = this; self.list = ko.observableArray(data); self.selected = ko.observable(); } ...

By utilizing // within the source of a <script>, one can efficiently reference external files without specifying the

Curious if anyone has come across any evidence, proof, or firsthand accounts of utilizing the traditional http/https JavaScript <script> hack: <script src="//someserver.com/js/script.js"></script> Have you faced any problems with this m ...

Maintain the modifications in NPM software dependencies

Currently, I am developing a REACT web application and have integrated the react-datasheet library using NPM. To ensure compatibility with IE11, I made modifications to the JavaScript file installed via NPM. While these changes work perfectly on my local m ...

The output from the lodash sortBy function is not aligning with the anticipated result

Sorting this array based on the attributes age and user has become my current challenge. The priority is to first sort by age, followed by sorting by user. In cases where the ages are the same, the sorting should be done based on the user attribute. var us ...

Hiding the Previous or Next Button on the First and Last Item in a Twitter Bootstrap Carousel

Is there a way to hide the previous button on the first item and hide the right button when the carousel reaches the last item? I've tried different solutions but couldn't get it to work. Any help in understanding how to implement this for a Boot ...

Exploring the characteristics of $scope elements generated by the $resource factory service within AngularJS

I need to access attributes of an object that originates from a $resource factory service (REST API). services.js: myApp.factory('userService', ['$resource', 'backendUrl', function($resource, backendUrl) { return $resource ...

The cause of Interface A improperly extending Interface B errors in Typescript

Why does extending an interface by adding more properties make it non-assignable to a function accepting the base interface type? Shouldn't the overriding interface always have the properties that the function expects from the Base interface type? Th ...

Directive in AngularJS fails to trigger upon form reset

I encountered an issue with a directive used within a form, specifically when clicking on a button with the type reset. The problem can be replicated on this stackblitz link. The directive I'm using is quite simple - it sets the input in an error stat ...