I would like to create an array within an object in Angular 2 in order to add new values to it

Every time I try to define an array inside the object using dot notation, it keeps showing as undefined

This is my HTML code:

<mat-checkbox  *ngFor="let q1 of question.checkboxes;" (change)="checkboxSelected(q1,question,$event)"> {{q1.name}} </mat-checkbox>

Here's my TypeScript code:

checkboxSelected(selected,obj,event){obj.box.push('somecondition') }

An error shows up saying:

ERROR TypeError: Cannot read property 'push' of undefined

Answer №1

Determine if the property "box" is present in the object named "question".

If it does exist, verify that it has been properly initialized, such as by setting question.box = [].

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

Processing a JSON array of objects in AngularJS

When using Angular's fromJson function to parse a JSON string, I encountered an issue. If the JSON is a simple array like "[1, 2]", the code works fine. However, I need to work with an array of dictionaries instead. var str = "[{'title' ...

When implementing the onchange function in PHP and JavaScript, the select list tends to overlook the initial value

My select list function in the code below creates a dropdown with 5 values. However, only values 2-5 work when selected, and their values are displayed on the page. Value 1 does not display no matter what I try. I have been unable to identify the issue or ...

In React Js, I have noticed that when I incorporate a Select field, it only works when the value is an integer and not a string. It seems

Library version "material-ui": "^1.0.0-beta.35" react version: "react": "^16.2.0" Currently Functional: <MenuItem value={1}>Text</MenuItem> <MenuItem value={2}>Integer</MenuItem> <MenuItem value={3}>Inline</MenuItem> N ...

Transform a delimited string and an array of objects into a new format

Is there a way to easily convert a delimited string into an array of objects with data binding functionality? ng-list is suitable for arrays of strings. However, I have an array of objects where I want to delimit the text property for easy editing. Works ...

How can I create boxes that expand when hovered over?

I am currently exploring ways to create a hover effect where divs or boxes open and animate downward when hovered over. I checked the Bootstrap documentation but couldn't find a built-in solution. Considering using JavaScript, but my knowledge in that ...

TypeScript is unable to identify the property causing the error

Recently, I came across a new feature in JavaScript where you can add a cause to the Error constructor. However, when attempting to utilize this feature in my application, it fails to start due to not recognizing this additional argument in the constructo ...

Is there a way to switch between modals with the same ID using JavaScript?

I currently have a modal window open which contains a paginated list. Within this modal, I have next/previous buttons that are supposed to display new content. Since this functionality needs to work on multiple pages, I can't include HTML code for th ...

reqParam = $.getQueryParameters(); How does this request work within an ajax form

I've spent a lot of time searching, but I can't figure out what the code reqParam = $.getQueryParameters(); means and how it is used in Ajax JavaScript. I copied this Java script from a website as an example. If anyone has any insights, please he ...

When I dispatch multiple actions, my effect is triggered only once

Whenever I load a group of drones, numerous events are dispatched. I want to dispatch a connection to an SSE service for each drone that is loaded. Here is my approach: drones.forEach((drone) => { console.log('dispatch', drone.id) ...

Oops! It seems like this issue is not related to npm. There may be more detailed logs available above this message

I am currently working on a project that utilizes Angular6 for the frontend. I am trying to deploy this project from a remote server to the actual server. To build the frontend, I am using the npm run build -prod command. However, I am encountering a recur ...

Issues with the Content Editable Functionality

While working on my website, I encountered a strange issue. For some reason, I can't seem to get the contenteditable="true" attribute to work on the heading with the ID "hl". It would be awesome if someone could help me figure out how to mak ...

Mastering the art of fetching data from a database for showcasing using a Chrome extension

Embarking on my first chrome extension development journey has been quite thrilling. The process involves an interesting workflow - once the extension is installed and activated, whenever a user hovers over a specific product/ID displayed on a webpage, the ...

Using regular expressions to replace all special characters, excluding dots

$('#price').keyup(function(){ $('#price').val($('#price').val().replace(/[_\W]+/g, "-")); }) Experience it firsthand here: http://jsfiddle.net/2KRHh/6/. In the scenario above, special characters are eliminated. ...

Create a NfcV Write Lock Block instruction

Seeking to make data on a NXP ICODE SLIX SL2S2002 tag type 5 (ISO 15693) read-only by utilizing the WRITE SINGLE BLOCKS command through the NfcV object in an app based on Ionic: private readonly cmdISO15693 = { READ_SINGLE_BLOCK: 0x20, WRITE_SI ...

Ensuring the stability and consistency of automatically generated serial numbers when sorting in ascending order in another column of the gridview

I am facing an issue with my list view on the page. There are three columns - serial number, row name, and row code. I have implemented sorting in ascending order for the row name using jQuery. However, when the sorting is applied, the serial numbers als ...

Passing custom variables from npm script to JavaScript

Is there a way to define variables in an npm script that can be accessed in JavaScript using process.env.CUSTOM_VAR? I have added "start": "react-native start --reset-cache" in my package.json file under the scripts section, which is w ...

replace the standard arrow key scrolling with a new custom event

Currently, I am in the process of creating a unique scroll box to address my specific needs. The standard html <select> box is not cutting it for me as I require two columns displayed, which I couldn't achieve with a regular <select>. Howe ...

Floating Elements Centralized

I have a relatively straightforward HTML layout with a heading div at the top, followed by a Main div that contains a NavBar on the left side and a ContentDiv on the right. The issue I'm facing is that I can't seem to get my NavBar and ContentDi ...

Tips for creating animated card designs with HTML, CSS, and JavaScript

As I delve into the realm of CSS animation, my primary focus is on replicating the captivating card shuffling animation showcased at Here's a glimpse of the progress I've made so far: https://youtu.be/GDIJ2K22cnY While I've successfully im ...

Customize the position of nodes and their descendants in a d3 tree chart by setting specific x and y coordinates

I am in need of a d3 tree structure that looks like this. https://i.sstatic.net/X6U3u.png There are two key points to understand from the image above: Headers will have multiple parents(wells). I need to be able to drag and drop links connecting w ...