Error encountered when attempting to destructure Object.entries() within a for-loop

Although I managed to get it to work, the perplexing part is trying to comprehend how this can occur!

This error message pops up: [ReferenceError: Property ‘date’ doesn’t exist]

for (const [date, items] of Object.entries(object)) { console.log(date) }

Now, check out this working version:

for (const entries of Object.entries(object)) {
    const date = entries[0]
    const items = entries[1]
    console.log(date)
}

The object in question remains constant each time it runs. It strictly contains only one entry, here's a peek at its contents when logged using Object.entries(object):

[
    "2022-01-02",
    {
        "a": [objects],
        "b": object,
        "c": [objects]
    }
]

In the functioning code block, I can observe the logging of date. However, attempting to log date after restructuring results in an error. It seems like the issue stems from my attempt to destructure the array. But why does this happen? Just so you know, I'm currently working with React Native.

Answer №1

One possible reason for the issue is that you might be redeclaring the variable items.

To resolve this, consider renaming the destructured variable to avoid conflicts:

const items = {
  '2023-01-02': [
    "value 1"
  ]
}

// This works fine
for (const [date, entryItems] of Object.entries(items)) { console.log(date) }

// Throws an error
for (const [date, items] of Object.entries(items)) { console.log(date) }

Answer №2

The reason for this is that the object you are working with is actually an array, which means you can only destructure it using indexes rather than keys.

let information = [
   "name",
   {
      "x":[items],
      "y": item,
      "z":[items]
   }
]

It's important to note that this data structure does not utilize keys but instead relies on indexes. That's why you won't find any entries with the keys name, properties, or details.

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

The functionality of my LinkButton activates a code sequence only when clicked twice during its Click event

Protected Sub lnkContractors_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkContractors.Click If Context.User.IsInRole("HOD") Then lnkContractors.OnClientClick = "PopupCenter('Juniors.aspx', &apo ...

Issues with React Router functionality on a live production site are causing complications

I recently created an Amazon-clone UI using create-react-app, but it only displays dummy data. The issue arises after deploying it to Vercel - the routing does not function as expected. When clicking on links, a blank page appears with correct URL paramete ...

How can we display the first letter of the last name and both initials in uppercase on the JavaScript console?

I'm a new student struggling with an exercise that requires writing multiple functions. The goal is to create a function that prompts the user for their first and last name, separates the names using a space, and then outputs specific initials in diff ...

The agent.add() function is malfunctioning, while console.log() is functioning properly

I'm currently working on integrating a bot using Telegram, Dialogflow, and Firebase. One particular function that I'm struggling with is as follows: function findDoc(agent){ const userId = request.body.originalDetectIntentRequest.payload.dat ...

several ngGrids and ngGridEventScroll

There are 2 separate ng grids on a single page, each with infinite scrolling that loads server-side data on ngGridEventScroll. scope.$on('ngGridEventScroll', function(event) { ... }); Each grid is designed to make its own unique server-side cal ...

Guide on populating a dropdown menu dynamically in php based on the selection made in another dropdown

Despite reviewing similar questions, I have not found a solution to my problem. I am attempting to retrieve a dropdown menu based on the selection of another dropdown. The first dropdown consists of school names, and upon selection, it should fetch the use ...

Tips for troubleshooting React issue (TypeError: Cannot read property 'showModel' of null)

Learn how to set the attribute showModel and give it a value of false: Main.js import React from 'react' import 'bootstrap/dist/css/bootstrap.min.css'; import Form from 'react-bootstrap/Form' import Button from 'react-b ...

finding the ID of the element that triggered a jQuery dialog

I'm utilizing jQuery dialog to trigger popup windows when buttons are clicked. <script> $(document).ready(function(){ $("#dialog-form").dialog({ autoOpen : false, height : 300, ...

Extract the titles and showcase them in a div using a structured list

Currently, I have a dynamic container with the class .product_section where I need to display the headings inside it in a UL list format within the div.toc. I am looking for a script that can iterate through each .product_section and generate a list using ...

The Google Maps marker is not accurately displaying the designated location

While working on my project, I successfully integrated Google Maps. However, I have encountered a problem: when I search for a specific location, the marker is not displaying at the correct point, but rather somewhere else. The latitude and longitude value ...

Angular code is failing to send a signal to the server following a $http.post request

I've been using angular for about a week now and I've been struggling to solve this issue. I have a service that wraps around $http because multiple controllers make calls to the same URL. On one particular page, there is a lot of server-side bus ...

The Springboot endpoint was successfully submitted through a fetch request, leading to a redirection to

Facing an issue while trying to submit a form to the /policy-holder page using fetch in JavaScript. The endpoint is redirecting to the login page, even though only admins are supposed to login. What could be causing this problem and how can it be resolved? ...

The jQuery click event to change the color function is functioning properly, but there seems to be an issue with

I'm having trouble changing the font-family using the code below, even though the color is changing properly. What am I missing here and how can I correct it? $(document).ready(function(){ $("body").on("click",".selectableColor", function(){ ...

Retrieving text that has been HTML-escaped from a textarea using jQuery

Within my HTML file, I have a <textarea id="mytextarea"></textarea>. Suppose a user inputs the following text: <hello>, world! How can I retrieve res = "&lt;hello&gt;, world!"; based on the user's input? The current code s ...

tips for efficiently loading JSON files using Angular

I currently have a situation where multiple controllers are calling the method getData() from a service. To prevent unnecessary duplicate http calls for the same json file, I have implemented the following approach: QuizApp.service('quizService' ...

``I am experiencing slow loading times with Google Maps JS when using

When accessing Google Maps on Safari, zooming in can be frustratingly slow on both Windows and Mac. The experience feels glitchy, with the zoom starting, freezing, then finally finishing. In comparison, Chrome provides a much smoother and faster zooming ex ...

What could be causing [object HTMLImageElement] to appear when attempting to update my favicon image?

Yesterday, I posted a question and received an incredibly helpful answer. Take a look at the question here. The response also includes the code snippet. However, when viewing my rendered HTML, instead of getting the link as expected, I am seeing <link ...

Is there an undocumented property lurking in the depths of the Webpack server

The document specifies that Options that work with webpack-dev-middleware will have a key symbol next to them. I couldn't find any information on "cookieDomainRewrite" but when I tried using it, it worked. Any insights? Or suggestions on how I ...

Learn how to efficiently send multiple image files to the server by utilizing Formidable and React JS

I am encountering an issue while attempting to upload five images to the server. The code file is provided below, and any assistance would be greatly appreciated. In this scenario, I am inputting image files and storing them in an array, but Formidable in ...

Posting a string using AJAX to the Java backend through JavaScript

I'm currently working on a comment section for a Web Client. There are two tables involved - one for the comments and another for the users. My goal is to send input data from HTML <input> tags to a Java Service where the data will be processed ...