I have the following array (the code is written in Java) :
String[][] a = new String[3][2];
a[0][0] = "1";
a[0][1] = "2";
a[1][0] = "1";
a[1][1] = "2";
a[2][0] = "1";
a[2][1] = "2";
and what I want to do is to print 111222 and I accomplished that in Java by doing this:
for (int i=0;i < a[i].length;i++){
for(int j=0;j <a.length;j++){
System.out.print(a[j][i]);
}
}
What is the equivalent of this in JavaScript?
Here is the equivalent code in Javascript (no space its not a script version of java)
! edit missed the particulars of the loops, fixed now
var a = [];
a.push(["1", "2"]);
a.push(["1", "2"]);
a.push(["1", "2"]);
for(var i = 0; i < a[i].length; i++) {
for(var z = 0; z < a.length; z++) {
console.log(a[z][i]);
}
}
var arr =[
[1,2,3],
[4,5,6],
[7,8,9]
],arrText='';
for (var i = 0; i < arr.length; i++) {
for (var j = 0; j < arr[i].length; j++) {
arrText+=arr[i][j]+' ';
}
console.log(arrText);
arrText='';
}
Output:
for (i=0; i < a.length; i++) {
for (j = 0; j < a[i].length; j++) { document.write(a[i][j]); }
}
Though it would be smarter to add all the strings together and the print them out as one (could add to an element or alert it out.)
In javascript you can create multi dimensional array using single
dimensional arrays.
For every element in an array assign another array to make it multi
dimensional.
// first array equivalent to rows
let a = new Array(3);
// inner array equivalent to columns
for(i=0; i<a.length; i++) {
a[i] = new Array(2);
}
// now assign values
a[0][0] = "1";
a[0][1] = "2";
a[1][0] = "1";
a[1][1] = "2";
a[2][0] = "1";
a[2][1] = "2";
/* console.log appends new line at end. So concatenate before printing */
let out="";
for(let i=0; i<a.length; i++) {
for(let j=0; j<a[i].length; j++) {
out = out + a[i][j];
}
}
console.log(out);
var a = [];
a[0] = [];
a[0][0] = "1";
a[0][1] = "2";
a[1] = [];
a[1][0] = "1";
a[1][1] = "2";
a[2] = [];
a[2][0] = "1";
a[2][1] = "2";
for (i = 0; i < a[i].length; i++) {
for (j = 0; j < a.length; j++) {
document.write(a[j][i]);
}
}
js is very powerfull ; just use spread operator
mat = [[1,2,3],[3,4,5]]
mat.forEach(v=>console.log(...v));
// output
run the code see the output.
1 2 3
3 4 5
Related
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';
}
}
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);
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.
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
}
How I can split a string into 2D array. String is like
1c2c3r4c5c6r6c8c9
array should be like
[[1,2,3],
[4,5,6],
[7,8,9]]
var src = "1c2c3r4c5c6r6c8c9";
var rows = src.split(/r/);
for (var i = 0; i < rows.length; i++)
rows[i] = rows[i].split(/c/);
Please note that I didn't test this so it might contain a syntax error or something...
You can use the map method on Array
var s = "1c2c3r4c5c6r6c8c9";
var rows = s.split("r");
result = rows.map(function (x) {
return x.split('c');
}));
map is introduced in ECMAScript5 and is not supported in older browsers. But, there is a decent work-around here
var str = "1c2c3r4c5c6r6c8c9";
var result = [];
var group = str.split("r");
for(i in group) {result.push(group[i].split("c"))};
result should be what you want.
This should work:
var src = "1c2c3r4c5c6r6c8c9";
var rows = src.split(/r/g);
for (var i = 0; i < rows.length; i++) {
var cells = rows[i].split(/c/g);
for (var j = 0; j < cells.length; j++) {
cells[j] = parseInt(cells[j]);
}
rows[i] = cells;
}