Generating a multi-dimensional array containing only the value 'false' in each sub-array

My goal is to create an array of arrays in JavaScript, with each inner array filled with the value false. However, I'm encountering an error message that says:

Uncaught TypeError: Cannot read property 'push' of undefined

The code I am currently using is as follows:

var fieldFilled = [];

for (var i = 0; i < 10; i++) {
    fieldFilled.push([]);
            
    for (var j = 0; j < 10; j++) {
        fieldFilled[j].push(false);
    }
}

I find it confusing because it appears that fieldFilled is defined. Any insights or assistance on this issue would be greatly appreciated.

Thank you for your guidance.

Answer №1

One possible solution is to utilize the functional programming technique:

const matrix = new Array(10).fill(0).map(row => new Array(10).fill(false));

console.log(matrix)

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

Determine the figures derived from a pair of input fields utilizing jQuery

I'm attempting to combine the scores from Level 1 and Level 2 and showcase the result within the <div id="score"></div>. So far, this is what I've come up with, but it doesn't work for all inputs and seems like a convoluted mess ...

setting a variable with data retrieved from an AJAX call using jQuery

Here's a snippet of jquery code that I'm working with: var example = "example"; $.ajax({ url: root + "/servletPath", type: "GET", success: function (response) { alert(response); // displays the correct value example ...

Can the page's user interface stay the same even after refreshing the page?

Are you considering including a test case to verify if all checkboxes are unchecked after the page reloads? Is it possible for checked checkboxes to remain selected even after reloading or navigating away and returning to the page? Could this issue be due ...

Creating a personalized bullet text box with HTML and Javascript: A step-by-step guide

I have been working on creating a customized text box using HTML and JavaScript that has specific requirements: The text box should start with a single bullet point, like this: https://i.sstatic.net/U7pHo.png Each new line entered by the user should autom ...

Guide to implementing controllers in vuejs2

Hey there, I recently started using vuejs2 with a project that is based on laravel backend. In my vuejs2 project, I wrote the following code in the file routes.js export default new VueRouter({ routes: [{ path: '/test', component: ...

Tips for Converting a JavaScript Array into JSON

I am dealing with data structured like this: "team": "Yankees" "players": ["jeter", "babe ruth", "lou gehrig", "yogi berra"] In my code, I extract these values from a form where they ar ...

The content selected in a bootstrap multiselect dropdown may break across multiple lines

I am currently using a bootstrap multiselect dropdown to display all the selected values in the dropdown. However, I now have a large number of values and would like to break them into multiple lines. This is how it currently looks like displayed in the im ...

Using Google Chart Tools to manage overlapping labels in visualizations

Currently, I am utilizing Google Chart Tools to showcase a basic line graph, but I am encountering an issue where the labels are overlapping regardless of how I configure the "legend" parameters. The screenshot below illustrates the outcome for legend: { ...

Tips for making multiple views function with angularjs' ui-router

I attempted to follow the instructions provided here but I am unable to make it work. https://github.com/angular-ui/ui-router/wiki/Multiple-Named-Views I made sure that my bootstrap grid divs were functioning correctly by placing them all in index.html an ...

Mongoose is unable to update arrays, so it will simply create a new array

Having trouble updating my collection without any errors. Can someone lend a hand? I've been at this for 3 hours now. const product_id = req.body.cartItems.product_id; const item = cart.cartItems.find(c => c.product_id == product_id); i ...

Can Javascript templates help improve server performance compared to PHP templates?

I'm in the process of developing a website that heavily relies on client-side technologies like Symfony2, Backbone.js, and Underscore. Symfony2 is used for the backend, while Backbone.js powers the frontend. In Symfony2, Twig templates are used and co ...

The axios post method does not return any parameters

I am facing an issue with my code where the productArray in axios always returns null. Strangely, it works perfectly fine when I use jquery. Can anyone guide me on what might be missing? $.post(`/api/${productId}/getProducts`, { products: productArray }) ...

Vuetify's data table now displays the previous and next page buttons on the left side if the items-per-page option is hidden

I need help hiding the items-per-page choices in a table without affecting the next/previous functionality. To achieve this, I've set the following props: :footer-props="{ 'items-per-page-options':[10], &apo ...

Objective-C arrays and the @interface declaration in Objective-C files

As I embark on my journey to learn Objective-C, I find myself delving into the code snippet provided below. This code is spread across three files: FindLargestNumber.h, FindLargestNumber.m, and main.m. Within the FindLargestNumber.h file, I am intrigued b ...

Django view receiving incorrect AJAX data

I am currently learning Django and AJAX through a project I have been working on. In this project, I have implemented two buttons - one to add a marker and another to delete a marker. Below is the code snippet from views.py @csrf_exempt def save(request) ...

When I click a button in d3 to refresh the data on my bar graph, the text fails to update accordingly

I've successfully created a series of data lists that modify the bargraph. Unfortunately, due to their differing x and y values, they end up printing new values on top of existing ones. Shown below is an image illustrating the issue where x and y val ...

Mobile devices have a fixed distance and center alignment on the horizontal timeline

I am attempting to create a horizontal timeline similar to this example: https://codepen.io/ritz078/pen/LGRWjE The issue I am facing is that I do not have specific dates (DD/MM/YYYY) but only ranges such as 1998-2002 or single years like 2009. This has ca ...

Is there a problem with this method of initializing std::array within the code?

Upon examining the given declaration: #include <array> struct X { //std::array<bool,3> arr={false,false,false}; bool brr[3]={false,false,false}; }; It's interesting to note that it compiles successfully using g++ 5.2. However, i ...

Transferring data from Laravel to the Vue.js frontend

In order to transfer PHP variables from Laravel to Vue frontend, I devised a JavaScript class. Within this class, I utilize the static put method to transmit an associative array to the frontend. <?php namespace App\Helpers; class Javascript { ...

End your Idp session and log out using passport-saml

Encountering a 400 bad request error when attempting to log out a user from the idp session. Despite successfully logging out the user from the application/passport session, they remain logged in to the idp session. The logout and callback endpoints are c ...