JavaScript Arrays are not functioning properly within the Poo constructor

Attempting to insert data into a JavaScript Array within my constructor has resulted in this error message:

TypeError: this.listeetudiant is undefined

This is my constructor:

function Cours(){

*/the array causing trouble: */
this.listeetudiant=[]; 

*/ the method intended to utilize the array's content and add data to it */
this.affForm=printForm; 

}

Subsequently, I attempted to add data to listeetudiant using the method, only to encounter issues:

function printForm(){
 this.listeetudiant.push = (1);
}

Furthermore, I attempted

this.listeetudiant.push(etudiant[id].id);

where etudiant[id].id is a valid variable...

Unfortunately, the issue persists and the error message "TypeError: this.listeetudiant is undefined" is displayed.

Please lend me a hand!

It functions properly when adding one data to a single variable. However, encountering challenges with arrays! I require the ability to insert multiple data into my array listeetudiant at various intervals, which seems unachievable.

For instance, if I write:

function printForm(){
this.listeetudiant=(1); 
alert(this.listeetudiant);
}

it performs as expected, with listeetudiant's value becoming 1 within my object. The dilemma lies in the necessity of adding multiple values to listeetudiant as an array — a capability I'm struggling to achieve!

Answer №1

The variable this.listeetudiant has not been properly defined. Although a constructor has been created, it has not been called, leaving this.listeetudiant undefined.
If Cours is meant to be an object, it needs to be called first:

var cours = new Cours();

Then, you can perform actions such as cours.listeetudiant.push();

function printForm(){
 this.listeetudiant.push = (1);
}

-- In this context, the this refers to the printform function. Whereas in the cours function, it represents the cours function itself. The use of this is dependent on the scope of the code. Since listeetudiant is an array attribute of the cours object, you must reference the object when pushing to the array.

Answer №2

Appreciation to everyone, including Jack, for the assistance provided.

I am grateful for the support, it finally worked out with this code snippet: function printForm(){ cours.listeetudiant.push('1','12'); }

For those curious, the data structure was an array of objects, so I had to modify it like so: cours[0].listeetudiant.push('1','12');

Greetings from France, have a wonderful day. I will offer my assistance whenever I can.

If you're interested in the project I'm working on, check out the code here:

Any idea how to mark an issue as resolved?

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

Setting the state variable value asynchronously using AngularJS UI-If

Is it possible to limit access to the sidebar until the user has completed the login process? The sidebar is located outside the main ui-view section I am assuming that authenticated is a state variable <div ng-controller="MyController" ui-prevent-to ...

Accessing PHP variables within a React componentTo access external

How can I pass the id selected in a menu from a react component to a PHP file, after already knowing how to send data from PHP to the react component? $.ajax({url: '/file.php', type: 'POST', dataType: 'json', ...

Stagnant variable value after onClick event

After exploring various solutions, none seem to quite fit my needs. I want to update the variable "currentIndex" when a user clicks on an image. Currently, the change occurs within the onClick function but does not affect the outside variable. I am unsur ...

Exploring Type Refinements with Flow

I keep encountering an issue indicating that 'The operand of an arithmetic operation must be a number.' despite having a runtime check at the beginning of the function to confirm that this.startedDateTime is indeed a number. I am puzzled as to wh ...

The button fails to trigger the AJAX request

I've developed a form that includes a QR code generator. The QR code is generated as an SVG, and below the code is a download button that should trigger an AJAX call to my PHP script when clicked. However, the download button does not seem to initiate ...

Is jQuery still recommended for adding animations in VueJS?

In my component's methods object, I currently have the following code snippet: startImageAnimation() { $('.splash-image').fadeIn(1400, () => { setTimeout(function() { $('.splash-image').fadeOut(1400, () ...

Remembering data in a lazy loaded AngularJs list

Encountering an issue with a lazy loaded list of books. Initially, 10 books are loaded and if the user scrolls down, the next 10 books are loaded. For example, if the user is on "page" 4 and clicks on the 35th book to view details, upon returning to the l ...

Error in delete operation due to CORS in Flask API

After successfully developing a rest api in Flask and testing all api endpoints with Postman, I encountered an issue while working on an application in Javascript that consumes the resources of my api. The problem lies in consuming an endpoint that uses t ...

How can you identify changes in localstorage and then trigger the activation of a new boot file or reallocate a value using Vue.js or JavaScript?

When users log in, they have the option to choose from 3 domains: dev, demo, and sandbox. The configuration for these domains is set in the Boot.js file using Axios. The boot file is triggered prior to the firing of the Vue instance. Below is the content o ...

I need three buttons, but I only want one to be active at a time. When one button is clicked and becomes active

function toggleSlideBox(x) { $("#"+x).slideToggle(300); } I am utilizing a JavaScript feature on buttons to create dropdowns that display forms, text, etc. When one button is clicked, it drops down along with the other two buttons, and when the ...

The issue arises when a VBA array containing class objects within an object surpasses the stack space limit,

I am attempting to demonstrate how to use an array within the Class Bookstore to store multiple book objects with the code provided below. Instead of using a collection, I prefer to utilize an array so that I can easily access Book(i) later on and perform ...

Adding JSON data to an array in Node.JS

I am facing an issue where my JSON data is successfully being appended to an array inside a JSON file. However, the problem is that it is not appending inside of the brackets in the JSON file. Below is the code snippet for my NODE if (req.method == &apo ...

Can you identify the issue with my database file?

Here is the content from my database.js file: const MongoClient = require('mongodb').MongoClient; const db = function(){ return MongoClient.connect('mongodb://localhost:27017/users', (err, database) => { if (err) return co ...

Preventing context menu from appearing on a right click by implementing a trigger to re-enable it

I am looking to implement a functionality on my website that disables the right click menu. Currently, I have achieved this through the following code: document.addEventListener("contextmenu", function(e){e.preventDefault(); }, false); Now, I am wonderin ...

What steps can be taken to ensure the random generator continues to function multiple times?

My little generator can choose a random item from an array and show it as text in a div. However, it seems to only work once. I'm looking for a way to make it refresh the text every time you click on it. var items = Array(523,3452,334,31,5346); var r ...

Creating a Dynamic Bar in Your Shiny Application: A Step-by-Step Guide

Currently, I am developing a unique crowd funding shiny app to monitor donation amounts. Is there a method available that allows for the creation of a reactive bar in Shiny? Alternatively, is it feasible to achieve this using html, css, and javascript? He ...

How to Easily Add GitHub NPM Packages to Your SAPUI5 Web IDE

Looking to integrate an NPM package from Github into SAPUI5 using the WebIde Framework. Package Link: https://github.com/commenthol/date-holidays/blob/master/README.md#usage Primary Issue: Need a file with the library for importing and copying into the W ...

Encountering a problem with MongoDB when trying to import a JSON file

I'm struggling to import my JSON file into MongoDB. Even though I don't see any errors like "missing { symbol," MongoDB keeps throwing an error at me. What should I do to fix this issue? I tried following a sample and made edits accordingly, but ...

How about generating a promise that serves no real purpose and simply resolves?

Managing promises in Node.js can be a bit tricky, especially when dealing with different scenarios. One common use case is catching the last promise result and formatting it accordingly. req.resolve = (promise) => { return promise.then(() => { ...

What is the best way to create a button that will trigger a modal window to display a message?

I am looking to create a button that will open a modal window displaying a message. However, when I tried to add a label and viewed the page, the desired window appeared on top of the rest of the content. But unfortunately, clicking the button did not prod ...