Struggling to populate a fresh JavaScript array with values submitted from a form

I am currently working on a form to collect Family information that will be used for enrolling in a new benefit plan. My goal is to populate an array with the necessary values for each individual to create the benefit record. While researching, I have only found methods that create array values and increase the index for each value. However, what I require is adding multiple elements to each array index.

Unfortunately, the initial code I attempted did not work as expected. Although I am able to retrieve values from the form, when attempting to create the array row, it results in errors. I am seeking guidance on how to successfully achieve this task.

Upon execution of my code, I received the error message "Unable to set property 'fname' of undefined or null reference."

I understand that my approach may not be conventional, but I am struggling to find the correct methodology to proceed. Can anyone provide insight into resolving this issue?

function ValidateChgFAM()
{   
formObj = self.MAIN.document.SSform2;

   var Fname = formObj.FirstName11.value
   var Lname = formObj.LastName11.value
   var Gender = formObj.Gen11.value

var famMbrs = new Array();
    famMbrs[0].fname = formObj.FirstName11.value
    famMbrs[0].lname = formObj.LastName11.value
    famMbrs[0].gender = formObj.Gen11.value
    famMbrs[0].birthdate = formObj.Birthdate11.value
    famMbrs[0].seq = "01"

Execution of this code led to another error message stating "Unable to set property 'seq' of undefined or null reference."

Although not overly familiar with Javascript, I am experienced in navigating through this system and problem solving. Any assistance in resolving this issue would be greatly appreciated.

Answer №1

An easier and more sophisticated approach:

let familyMembers = [];

const member = {
  firstName: formObj.firstName.value,
  lastName: formObj.lastName.value,
  gender: formObj.gender.value,
  birthdate: formObj.birthdate.value,
  sequence: '01'
}

familyMembers.push(member)

This will yield:

Array(
  0: {
    firstName: (value),
    lastName: (value),
    gender: (value),
    birthdate: (value),
    sequence: '01'
  }
)

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

Fetch additional data from a table by utilizing Ajax and PHP

I have 7 data entries in my database When attempting to load the data from a table using ajax and php, I encountered an issue. After clicking the "load more" button, the data displays successfully but the "load more" button disappears. Here is my index. ...

Jquery code not responding to ajax callback

i have the following two functions defined: function populateTableData(){ jQuery('select[name="drg-select"]').change(function() { drgValue=jQuery(this).val(); url="http://localhost/ur ...

Trouble with Bootstrap Collapse feature not collapsing on its own

I recently added a Bootstrap collapse feature to my payment view in Laravel. The Bootstrap section collapses when clicked, but I would like it to be collapsed by default. I understand that I need to include: aria-expanded="false" However, it still doesn& ...

Java - Getting the index of a list element as an integer

I'm having trouble figuring out how to retrieve all the values from my list that match a specific input. I'm not familiar with how to determine the position of each value in the list (e.g., if it's at position 0, 1, 2, etc.) based on whether ...

An error was encountered when attempting to reference an external JavaScript script in the document

Could someone please provide guidance on how to utilize the document method in an external .js file within Visual Studio Code? This is what I have tried so far: I have created an index.html file: <!DOCTYPE html> <html lang="en"> <head> ...

Utilizing jQuery to Determine Character Count

I've tried everything on the site, but nothing seems to be working. I'm attempting to create a function that triggers when the character count in a div exceeds a certain number, but it's not functioning as expected. Any assistance would be g ...

The variable 'form' has not been assigned an initial value in the constructor of the property

Below is the snippet from my component.ts file: import { Component, OnInit } from '@angular/core'; import { Validators, FormControl, FormGroup, FormBuilder } from '@angular/forms'; @Component({ selector: 'app-license', te ...

Tracking the referral source when the email box is clicked

document.referrer is a JavaScript method that returns the URI of the page that linked to the current page. If the user navigated directly to the page, for example through a bookmark, the value returned by document.referrer will be an empty string. You can ...

Instructions for running a script from a personalized NPM package

I have developed a unique NPM package that includes a script I want to run from another project. Here is an overview of my package: myScript.js console.log("The script has been executed!"); package.json { "name": "@myscope/my-script", "version": "0. ...

The first element of the JSONArray in Android is not a JSONObject

I am currently working on an Android project and I have encountered a JSON parsing issue. I have validated my JSON using JSONLint.com and it seems to be correct. { "traces": [ "{\"lat\": \"42.842097\", \"lng\": &b ...

Working with node.js to set up an ordering function

I am working with node.js and express3. To use mongodb, I decided to install the mongo-lazy package. This is the simple GET router I have set up: var db = require('mongo-lazy').open({ db: 'somedb', host: '127.0.0.1' ...

Creating a predictive text feature using AngularJS and jQuery

Currently, I'm developing a web application that utilizes: JQuery AngularJS Kendo framework Specifically, I am tasked with implementing an auto-complete feature using Kendo on an input text field. Initially, my approach was to target the inp ...

Leveraging Environment Variables in Separate JavaScript Files within a Node Express Application

After trying various methods and searching extensively online, I am struggling to use environment variables in a separate JavaScript file within an Express App. My Setup: In my Express app, I have an index.js file that serves an HTML page with associated ...

Guide to retrieving a string value rather than Json output with a mongodb aggregate function

I have a function that retrieves the value of the 'minHospitalization' field from the database. The response from this function is '[{"minHospitalization":1}]' Instead of returning the value in JSON format, I want to return just ' ...

Deduct x days from a Date String in a React Component

How can I subtract a specified number of days from a date that is in string format, like "2021-07-19"? I attempted to convert the string into a date, subtract the days, and then convert it back to a string, but this method did not produce the desired resu ...

Display with organized information, Arrange with unprocessed data in DataTables.net

Below is a snippet of the datatables configuration I am working with: { "dom" : "rltip", "processing" : true, "serverSide" : false, "order" : [ [ 1 , "desc" ] ], "searching" : false, data: [ { "column-a" : "Samp ...

I am struggling to extract data from the spawned Node.js child process. What am I overlooking?

Trying to utilize a spawned command-line process for lzip in order to expand an lzipped data stream due to the lack of suitable native JavaScript tools. Succeeded in achieving this by working with files and file descriptors, although cumbersome to handle ...

Using ng-repeat with ng-model; accessing the value of ng-model

I find myself in a peculiar situation - Behold the Controller snippet - $scope.filterNum = {0: 'filterVal1', 1: 'filterVal2', 2: 'filterVal3', 3: 'filterVal4', 4: 'filterVal5', 5: 'filterVal6', ...

Optimal Method for Updating Tags Across Three Tables in Node.js and MySQL

Currently working on developing an application that allows users to create and share content, referred to as "moments", along with relevant tags. Utilizing a MySQL database, I have designed a 3-table data model structured as follows: Table: MOMENTS, Col ...

Steps for eliminating the chat value from an array tab in React

tabs: [ '/home', '/about', '/chat' ]; <ResponsiveNav ppearance="subtle" justified removable moreText={<Icon icon="more" />} moreProps={{ noCar ...