Generating a two-dimensional array and setting its values in JavaScript

I need assistance with creating and initializing a two-dimensional array in JavaScript within an AngularJS application. My current approach is as follows:

$scope.invalidVote = [];
for (var i = 0; i < $scope.arry1.length; i += 1) {
    $scope.answersCount[i] = $scope.arry1[i].arry2.length;

    for(var j = 0; j < $scope.arry1[i].arry2.length; j += 1) {
        $scope.invalidVote[i][j] = false;
    }
}

However, this implementation is not working as expected. Can someone please guide me on the correct way to achieve this?

Answer №1

Here is a suggestion:

$scope.invalidVote = [];
for (var i = 0; i < $scope.arry1.length; i++) {
    $scope.answersCount[i] = $scope.arry1[i].arry2.length;
    $scope.invalidVote[i] = [];

    for(var j = 0; j < $scope.arry1[i].arry2.length; j++) {
        $scope.invalidVote[i][j] = false;
    }
 }

Answer №2

Assuming that $scope.arry1[i] is an array filled with other arrays and values, your code should be structured as follows:

$scope.invalidVote = $scope.arry1;
for (var i = 0; i < $scope.arry1.length; i += 1) 
  {
    $scope.answersCount[i] = $scope.arry1[i].length;
    for(var j = 0; j < $scope.arry1[i].length; j += 1)
      {
        $scope.invalidVote[i][j] = false;
      }
  }

By setting '$scope.invalidVote = $scope.arry1;', you are ensuring that "invalidVote" will have the same number of indexes.

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

Using jQuery's sortable functionality to rearrange rows in a table can cause conflicts with Angular's orderBy feature

In the past, my angular app used a table with orderBy to sort rows based on a specific column. By clicking on a table header, the orderBy arguments would change and the list would be sorted according to the values in that column. Now, I am experimenting w ...

"Implement highcharts redraw() method to update the chart, along with a callback function that interacts

I am working with a chart that utilizes the events.load function to draw lines based on the properties of the chart. The load function is functioning as expected, but I want to erase and redraw the lines each time the chart is redrawn, such as when hiding ...

The interlocking web of Angular dependencies

Can I begin my angular module without specific dependencies? This is my angular.module. angular.module("app", [ 'ngSanitize', 'ngAnimate', 'ui.router', 'ngMaterial', 'ngRoute', 'ngCookies', &a ...

If you don't get the correct response from the $.ajax success function

I am utilizing the $.ajax function to retrieve content, however I am encountering an issue when attempting to display special tags from it. The data appears to be missing! This is how I am currently handling it: $(document).ready(function(){ $("button") ...

Issue: The observer's callback function is not being triggered when utilizing the rxjs interval

Here is a method that I am using: export class PeriodicData { public checkForSthPeriodically(): Subscription { return Observable.interval(10000) .subscribe(() => { console.log('I AM CHECKING'); this.getData(); }); } ...

Angular: Assign a value to ng-model using a variable from the scope

I am trying to set a default value in my ng-model from the scope, but for some reason it's not recognizing the value. Here is what I have in my controller: $scope.defaultRole = "Admin"; This is how my view looks like: <input type="text" class= ...

Tips for handling daily JavaScript tasks using Angular JS

Over the past few days, I've been diving into AngularJS. While it seemed intuitive in tutorials and videos, when I actually started replacing my current web app code with AngularJS, I encountered numerous issues. For instance, if I wanted to add HTML ...

Sharing data between pages in Ionic and Angular with POST requests

Currently, I am utilizing Ionic for developing a mobile application and have decided to incorporate very basic authentication (without any security measures) into the app. The process involves validating user credentials against a database through a POST r ...

Exploring AngularJS: Leveraging ng-model within a custom directive featuring iterations and dynamically generated HTML elements

Trying to implement a directive for a grid, I encountered an issue where passing in a column definition that includes an HTML control with ng-model and ng-click directives resulted in an error: "Error: [$rootScope:infdig] 10 $digest() iterations reached. ...

Connecting Ag Grid with modules

Unable to link with modules as it's not a recognized attribute of ag-grid-angular <ag-grid-angular #agGrid style="width: 100%; height: 100%;" id="myGrid" class="ag-theme-balham" [modules]="modules" [columnDefs ...

The PHP code encountered a syntax error due to an unexpected $EOF on the final empty line

I've been tasked with maintaining an old website that went offline due to script errors. I managed to resolve most of the issues in the script, but there's one error that's giving me trouble. The site is showing a syntax error that says "une ...

Changing the 'badge' to 'panel' within the UI framework of 'ant design' has been set

Can the Badge be placed next to 'Info' in a Panel using ant design? View Code <div> <Collapse> <Panel header="Info" key="1"> <Badge count={4} style={{ backgroundColor: "#fff", ...

Adapting the position of a table row in AngularJS based on the

I need assistance with creating a dynamic table-row that moves to indicate the current time in a table filled with timestamps. <table> <tr ng-repeat="timestamp in timestampArray"> <td>{{timestamp}}</td> </tr> ...

AngularJS Basic Authentication with 'Access-Control-Allow-Origin'

function loginSuccessFn(data, status, headers, config) { Authentication.setAuthenticatedAccount(data.data); //window.location = '/'; return $http.post('https://ap-codereview.us.oracle.com/api/json/accounts/login', { ...

The loading of angular.min.js.map fails in Ionic

While running my Ionic app in the iOS simulator and using the Safari Web inspector, I encountered the following error: file:///.../.../Application/.../myApp.app/www/lib/ionic/js/angular.min.js.map Failed to load resource: The requested URL was not found o ...

Trouble with storing data in Angular Reactive Form

During my work on a project involving reactive forms, I encountered an issue with form submission. I had implemented a 'Submit' button that should submit the form upon clicking. Additionally, there was an anchor tag that, when clicked, added new ...

SB Admin 2 menu with multiple levels does not collapse as expected

I'm in the process of integrating the menu from the sb admin 2 template into my Ruby on Rails application: As I gradually added components to test functionality, I successfully implemented the top and side nav bars. However, I encountered an issue wi ...

Ways to filter out specific fields when returning query results using Mongoose

I was wondering about the code snippet below: Post.create(req.body) .then(post => res.status(201).json(post)) .catch(err => res.status(500).json(err)) While this code works perfectly, I am curious about excluding a specific field, such as the __v fi ...

Establish boundaries for D3.js circle reports

I am currently working on a visualization project where I want to arrange cells with higher values to appear towards the top and left, similar to a gravity force. However, I am facing difficulties in keeping multiple circles within the boundaries of the re ...

Is it better to Vuex - manipulate store item twice, trigger new items, or perform transformations within components each time they are mounted?

I am considering performing two separate transformations on a single, large store item and then saving the results as two new store items. For instance: setEventsData: (state, data) => {...} // main huge master object // perform transformations on it an ...