Create and initialize bidimensional array in javascript - javascript

I am trying to create and initialize a bidimensional array in javascript inside an AngularJS application 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;
}
}
But it doesn't work, What is the right way to do that?

try this:
$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;
}
}

I'm assuming $scope.arry1[i] is an array that contain other arrays and is already fill with values.
So your code should look like:
$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;
}
}
'$scope.invalidVote = $scope.arry1;' declaring "invalidVote" like this ensure it contains the same amount of indexes.

Related

How to create a 2d array with array of objects inside columns in javascript?

I want to predefine the 2d-array containing an array of objects like this below image:
I am trying this way:
var grid = [];
iMax = 3;
jMax = 2;
count = 0;
for (let i = 0; i < iMax; i++) {
grid[i] = [];
for (let j = 0; j < jMax; j++) {
grid[i][j] = count;
count++;
}
}
In your code you're building up the two-dimensional array, but you are filling the rows with numbers (i.e. your setting the count) instead of an array of objects. So if you want to achieve the exact same structure as provided in the screenshot, you can do:
const rows = 4;
const cols = 4;
const elementCount = 3;
function buildGrid(rows,cols, elementCount) {
const grid = [];
for(let i=0; i < rows; i++) {
grid[i] = [];
for(let j=0; j < cols; j++) {
grid[i].push(Array.from({length:elementCount}, () => ({})));
}
}
return grid;
}
const grid = buildGrid(rows,cols,elementCount);
console.log(grid);

Why I can't assign values to a emtpy array with a For cicle?

I am learning to use Javascript. I tried to fill an empty matrix, through a For cycle. I think that the sentence I declare has logic, and it should work, I have something intrigued. Anyone know the reason why Javascript does not work with this code?
I am learning to use Javascript. I tried to fill an empty matrix, through a For cycle. I think that the sentence I declare has logic, and it should work, I have something intrigued. Anyone know the reason why Javascript does not work with this code?
var i = 0 , j = 0;
var arr = [[],[]];
for( i = 0; i < 8; i++){
for( j = 0; j < 8; j++){
arr[i][j] = 7;
alert(arr[i][j]);
}
alert(arr[i][j]);
}
This should do what you are looking for.
var ar = new Array(10)
for(i = 0; i < 10; i++) {
ar[i] = new Array(10)
}
for(i = 0; i < 10; i++){
for(j = 0; j< 10; j++) {
ar[i][j] = 1
}
}
console.log(ar)
Check if the element that you want to use exists, if not, create it
var i = 0;
var j = 0;
var arr = [];
for( i = 0; i < 8; i++) {
for( j = 0; j < 8; j++) {
arr[i] = arr[i] || []; // Create array if needed
arr[i][j] = 'value';
}
}

Working with a problem of pushing nested arrays to an empty array

I'm working with the problem below:
You are provided with an empty array called nestedArr. Using a for loop starting at index 0, add 5 subarrays to nestedArr, with each nested array containing the string 'loop' concatenated with the corresponding index in nestedArr as its first element, and the index as its second element.
Example of a subarray: ['loop3', 3].
This is what I've tried
const nestedArr = [];
for (var i = 0; i < 5; i++) {
nestedArr[i] = []
for(var j = 0; j < 2; j++) {
nestedArr[i][j] = 'loop'+[i], i
}
}
console.log(nestedArr)
It gives me [['loop0', 'loop0'], ['loop1', 'loop1'], ['loop2', 'loop2'], ['loop3', 'loop3'], ['loop4', 'loop4']]
Can't figure out how to have it in this format: ['loop3', 3].
Please guide me!
You can just construct the array literally or use a ternary operator to check the first index in the inner loop and concatenate the string loop
const nestedArr = [];
for (var i = 0; i < 5; i++) {
nestedArr[i] = ['loop' + i, i];
}
const nestedArr2 = [];
for (var i = 0; i < 5; i++) {
nestedArr2[i] = [];
for(let j = 0; j < 2; j++){
nestedArr2[i][j] = j == 0 ?'loop' + i : i;
}
}
console.log(nestedArr, nestedArr2)
const nestedArr = [];
for (let i = 0; i < 5; i++) {
nestedArr.push(['loop' + i, i]);
}
console.log(nestedArr);

node js displaying a 2d array as grid

here is the code for making a 2d array to be used as a board for conways game of life in node.js. i am having a problem with displaying the board. the output looks like this.
['-','-','-']
['-','-','-']
['-','-','-']
however i want it to look like this
---
---
---
this is the code right now. Does anyone have any suggestions?
var createBoard = (width, height) => {
board = [];
row = [];
for (var i = 0; i < width; i++) {
for (var j = 0; j < height; j++) {
row.push("-");
}
board.push(row);
row =[];
}
return (board);
}
var displayBoard = (board) =>{
for (var i = 0; i < board.length; i++) {
console.log(board[i]);
}
}
gameBoard = createBoard(3,3);
displayBoard(gameBoard);
You need to join the elements of the array to form a string.
var createBoard = (width, height) => {
board = [];
row = [];
for (var i = 0; i < width; i++) {
for (var j = 0; j < height; j++) {
row.push("-");
}
board.push(row);
row = [];
}
return (board);
}
var displayBoard = (board) => {
for (var i = 0; i < board.length; i++) {
console.log(board[i].join(""));
}
}
gameBoard = createBoard(3, 3);
displayBoard(gameBoard);
To fix your issue you should iterate over the array and add it to a string, like this:
var displayBoard = (board) =>{
var buffer = '';
for (var i = 0; i < board.length; i++) {
for (var x = 0; x < board[i].length; x++) {
buffer += board[i][x];
}
buffer += '\n';
}
console.log(buffer);
}
This should print it like this:
---
---
---

Javascript can't convert undefined to object

I have the following code
for(i = 0; i < num; i++) {
var yPos = 10*i;
var numCells = wid/30;
for(j = 0; j < numCells; j++) {
blocks[i][j] = 1;
}
}
With
blocks = new Array();
However, when I execute the code I receive an error stating that:
can't convert undefined to object
Any ideas? :/
var blocks = [];
for(i = 0; i < num; i++) {
var yPos = 10*i;
var numCells = wid/30;
blocks[i] = []; // here is a fix
for(j = 0; j < numCells; j++) {
blocks[i][j] = 1;
}
}
In your particular case, since all the rows are initialised to be the same (a series of 1s), you can also do
var blocks = new Array(),
blockRow = new Array();
for (var i = 0; i < numCells; i++) {
blockRow.push(1);
}
for (var i = 0; i < num; i++) {
blocks.push(blockRow.slice()); // slice() makes a copy of blockRow
}

Categories