Values within a Javascript object are appearing as undefined

To create a structure similar to this:

params (Object) (defaults to: {}) —
    Delete — required — (map)
        Objects — required — (Array<map>)
            Key — required — (String) Key name of the object to delete.

This is how it's being implemented:

var params = {
    Bucket : data.bucket,
    Delete: [{
    Objects:[{ Key:""}] }]  
};

for(i=0;i<data.keys.length;i++){
    params.Delete[0].Objects[i].Key = data.keys[i];
}

An issue arises when setting the Key with

params.Delete[0].Objects[i].Key = data.keys[i];
                                ^

TypeError: Cannot assign value to 'Key' of undefined

Why is this error occurring?


It appears that there is only one instance of Key within Objects. I would like to include multiple keys inside Objects in order to construct the object properly. How can this be achieved?

Answer №1

There seems to be confusion about the number of object literals in the Objects array. Looks like only one is initialized, but you are using a for loop indicating expectation of more than one.

The error occurs when Objects[i] is undefined for the value of i at that point.

To resolve this, try adding new object literals into your Objects array as you process data by adjusting your code like so:

 for(i=0;i<data.keys.length;i++){
    params.Delete[0].Objects.push({Key: data.keys[i]})
 }

This adjustment will ensure new object literals are properly added to your Objects array during data processing (make sure to initialize params with an empty array for Objects initially).

Answer №2

When the index is 1, your params.Delete[0].Objects contains only one object. You're iterating through a for loop based on another loop with two different lengths.

data.keys

Contrastingly:

params.Delete[0].Objects

The latter has just one object, while the former has multiple objects.

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

Could a web-based ACM judging platform be created with Node.js or Python?

Suppose I am looking to develop a traditional online judging system, where the main components include: Receiving users' code and saving it to a file Compiling the code on the server Executing it on the server (while implementing sandbox measures to ...

Employ a adjusted column or partitioned table within the database

I need to store the following information in a MySQL database for notifications: id: VARCHAR(10) from_id: VARCHAR(10) to_id: VARCHAR(10) type: int(2) type_info: (The issue lies within this column) date : TIMESTAMP The type_info column's content is d ...

Is there a way to load JSON data into charts through an AJAX request?

Is there a way to pass JSON data to a JSP file and populate it into the charts data directly without setting each value using a model attribute? In my EmployeeController, I have a lineBarChart method that currently sets data values individually. Here' ...

What is the best way to use a regex to filter the user's input in a Vuetify combobox and generate a chip?

I am working on implementing a restriction for a specific regex pattern while the user types in a combobox to add new chips, such as allowing only phone number chips. Complete Vue Source code: https://codesandbox.io/s/chips-so-0gp7g?file=/src/domains/ex ...

Sending image to the server with the help of JavaScript

Curious if there is a method to upload an image to the server using javascript or jQuery and then save the image path/name into a database. I am working on a Windows platform server in asp.net 1.1, revamping a web page that is 10 years old. Unfortunately, ...

uncovering the secrets of tree shaking when using an npm package in conjunction with webpack

Imagine having an npm package where all components are exported from a single index.js file: export * from './components/A'; export * from './components/B'; Now consider another package that relies on this initial package: import {A} ...

Unusual actions when making a $.ajax call using the PUT method

When making a call to $.ajax, I use the following code: $.ajax({ type: 'PUT', url: model.url(), data: {task: {assigned_to: selected()}}, contentType: 'application/json' }) The function selected() returns an array. However, th ...

Retrieve the value of a cell in Google Sheets

Seeking assistance on how to retrieve the content of a cell in Google Sheets and store it for comparison purposes at a later time. My search efforts have led me to various online resources such as the Visualization API and the Sheets Script, but I have n ...

Having trouble getting dayjs to work in the browser with Vue.js?

Trying to display date differences in a human-readable format using the guide found here: I'm attempting to incorporate DayJS as a component in my VueJS application like this: <script src="{{ asset('/vendor/vuejs/vue.js') }}" t ...

What is the best way to utilize v-select for searching entries while also allowing users to type in text within the input field for the search?

How can I implement a fold-out display for entries in a v-select field while also enabling text search functionality to find specific items? Are there any Vuetify props that address this, or do you have any suggestions on how to approach this? <v-sele ...

What are the steps for incorporating more intricate validation criteria?

I would like to enhance the validation in this code by ensuring that the zip code field contains 5 digits that are only numbers, and the phone number field has at least 10 digits with no letters except for () or -. How can I incorporate this additional v ...

Unselect a radio button

I am currently utilizing the bootstrap radio buttons and I want to implement a feature that allows for deselecting a radio group. I came across an approach in this (Fiddle), which involves using an additional button. Instead of adding a separate button, my ...

Why do static files in Node.js + Express.js on Windows take up to two minutes to load?

I'm encountering an issue in my Windows environment with Node.Js/Express.js where static JS files can sometimes be labeled as 'pending' in the browser (even with caching disabled) for up to two minutes before finally downloading successfully ...

Add JSON data to an array of movie clips using ActionScript 3.0

I'm currently developing an air app in flash cs6 using as3. I have a requirement to send a JSON to a movieclip and integrate a "timeline" feature into my application. Below is the code snippet I am working with: function onCompleteLoadTimeline(event: ...

Handling JSON Data in JavaScript

In the script below, I have a json object that is being processed: $http({ url: '/mpdValidation/mpdValidate', method: "POST", data: { 'message' : mpdData } ).then(function(response) { console.log(response.data ...

Using AngularJS to interact with resources by passing in the scope parameter

My issue is as follows: I am attempting to call a resource with a specific parameter, but I keep encountering the following error: [$resource:badcfg] I have spent the last 3 hours trying to resolve this, but to no avail. When I call the resource like th ...

Node JS encountered an error: ECONNRESET was read at TCP.onStreamRead on line 211, column 20 in the node internal stream base commons

I am facing an issue while trying to insert a large array of data into my MySQL database using node.js. Everything works fine when dealing with smaller arrays, around 300 elements, but as soon as I try to insert an array with 1 million elements, I encounte ...

developing a grid system with JavaScript

I'm looking for an API that can help me build a datagrid similar to the one found at the following link: ...

Jackson will not convert null values using a personalized Serializer

One issue I encountered was with a custom bean serializer in Jackson that resulted in null properties being excluded from the output JSON. To reproduce the problem, consider the following code snippet: // Code example goes here Upon running the above cod ...

What is the best way to remove an item from my online shopping cart using JavaScript?

I am currently developing an online store website. One issue I am facing is deleting items from the cart after a customer completes an order. Below is the array of cart items: const products = [ { id: '0', name: 'Nike Slim Shirt&ap ...