how do loop through this 3x3 grid of arrays horizontally and print out 1, 4, 7, 2, 5, 8, 3, 6, 9?
edit: is there a more efficient way of doing this other than to use a two loops that also works if the length of the arrays are not equal to the other ones?
let grid = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
You can use nested for Loops:
let grid = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
];
for(let i = 0; i < grid[0].length; i++){
for(let j = 0; j < grid.length; j++)
console.log(grid[j][i]);
}
You have a lot of answers that work if the arrays are all the same length. Here's a way to handle potentially different length arrays. It basically runs until it can't find any more data:
let grid = [
[1, 2, 3, 4, 5],
[4, 5, 6, 7],
[7, 8, 9, 10, 11, 12]
];
let current = grid, i = 0
while(current.length){
current = grid.filter(arr => i < arr.length)
current.forEach(arr => console.log(arr[i]))
i++
}
for(var i = 0; i < grid.length; i++) {
var arr = grid[i];
for(var j = 0; j < arr.length; j++) {
console.log("array[" + i + "][" + j + "] = " + arr[j]);
}
}
you just have to find the sizes of the inner array and outer array
for(let i=0;i<countYourArraySize;i++)
{
for(let j=0;j<countYourInnerArayLength;j++)
{
console.log(grid[j][i])
}
}
let grid = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
let gridLength = grid[0].length;
let gridHeight = grid.length;
for(let l1=0; l1<gridLength;l1++) {
for(let l2=0; l2<gridHeight;l2++) {
console.log(grid[l2][l1]);
}
}
Assuming grid is rectangular(and grids usually are ;)), you can loop through it, columns first, rows second like in this snippet.
There was a question if we can do it without 2 loops, we can and I think its actually better and cleaner solution:
let grid = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
let gridLength = 3;
for(let l1=0; l1<gridLength*gridLength;l1++) {
console.log(grid[l1%gridLength][Math.floor(l1/gridLength)]);
}
You can do it with just one (explicit) loop if you use map:
let grid = [
[1,2,3],
[4,5,6],
[7,8,9]
];
let result = [];
for (let i=0; i < grid.length; i++) {
result = result.concat(grid.map((row) => row[i])));
}
console.log(result);
You can do this in a single loop. But the length must same for this
let grid = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
for (var i = 0; i < grid.length*grid.length; i++) {
var row = Math.floor(i / grid.length);
var col = i % grid.length;
console.log(grid[col][row]);
}
You need to make use of a nested for loop:
So please have a look at my solution:
let grid = [
[1,2,3],
[4,5,6],
[7,8,9]
];
for (let x = 0; x < grid[0].length; x++) {
for (let y = 0; y < grid.length; y++) {
console.log(grid[y][x]);
}
}
This grid prints out the expected solution: 1, 4, 7, 2, 5, 8, 3, 6, 9?
Some explanation:
In this case we are using two different for-loops.
the outer one for the X for (let x = 0; x < 3; x++) {}
the inner one for the Y for (let y = 0; y < 3; x++) {}
Note: This will just work if the grid is rectangular because using grid[0].length will otherwise produce an error.
Edit: If you just want to use a single loop try something like this:
let grid = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
for (let i = 0 ; i < grid.length * grid[0].length ; i++)
console.log(grid[i % grid.length][parseInt(i / grid.length)]);
Related
Forexample, I have an array like this:
const arr = [
[1, 2, 8],
[8, 5, 6],
[7, 8, 9]
];
And I want to remove the number 8 from bottom to top like dropping. Forexample:
const arr = [
[null, null, null],
[1, 2, 6],
[7, 5, 9]
];
I will replace the nulls later.
Loop through your array from the bottom to the top, then inside loop over each row. If the current item you find is an 8 - then drop all previous items in the same row down by one position, and set null on that position for the first row.
const arr = [
[1, 2, 8],
[8, 5, 6],
[7, 8, 9]
];
for (let i = arr.length - 1; i >= 0; --i) {
for (let j = 0, l = arr.length; j < l; ++j) {
if (arr[i][j] == 8) {
for (let k = i; k > 0; --k) {
arr[k][j] = arr[k - 1][j];
}
arr[0][j] = null;
}
}
}
console.log(JSON.stringify(arr));
I am trying to write a function that will take an array and n as parameters,
it will return all subsets of that array with n elements, have tried a couple things, couldn't yet succeed.
thanks to whoever put it here, this functions is way too complicated and doesn't do the job, basically what I tried to do here is to pick out one element from a 4 element array to create its 3 element subsets. It doesn't even take N as parameter. it returns all 3 element subsets but also identical ones, so I have to filter them out as well, in any case I will keep trying.
function findSubsets(array) {
var answers = [];
var firstArray = array;
for (i = 0; i < array.length; i++) {
array = firstArray;
for (var k = 0; k < array.length; k++) {
if (k != i) {
var subset = array.splice(k, 1);
answers.push(array); array.splice(k, 0, subset[0]);
}
}
}
}
That not as complicated as it seems. This one is optimized because it doesn't creates useless temporary arrays during the process.
function findSubsets(array, n) {
var answers = [];
for(var i = 0 ; i < array.length ; i += n) {
answers.push(array.slice(i, i + n));
}
return answers;
}
findSubsets([1, 2, 3, 4, 5, 6, 7, 8, 9], 2) // --> [[1, 2], [3, 4], [5, 6], [7, 8], [9]]
findSubsets([1, 2, 3, 4, 5, 6, 7, 8, 9], 3) // --> [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
You can try this solution
var subsetArray = (function() {
return {
getResult: getResult
}
function getResult(array, n) {
function isBigEnough(value) {
return value.length === n;
}
var ps = [
[]
];
for (var i = 0; i < array.length; i++) {
for (var j = 0, len = ps.length; j < len; j++) {
ps.push(ps[j].concat(array[i]));
}
}
return ps.filter(isBigEnough);
}
})();
var arr = [1, 2, 3, 4,5,6,7,8,9];
console.log(subsetArray.getResult(arr,2));
This question already has answers here:
How to get the difference between two arrays in JavaScript?
(84 answers)
Simplest code for array intersection in javascript
(40 answers)
Closed 1 year ago.
I wanted to compare two array and store the element that is present in both array into a new array. So I write this code but it didn't work.
var sampleArray = [1, 2, 3, 4, 5, 6, 7];
var sampleArray2 = [5, 6, 7, 8, 9, 10 ,11];
var similarElements =[];
for (let i = 0; i < sampleArray.length; i++) {
for (let j = 0; j < sampleArray2.length; j++) {
if (sampleArray[i] === sampleArray2[j]) {
similarElements.push();
}
}
}
console.log(similarElements);
let arr1 = [1, 2, 3, 4, 5, 6, 7];
arr2 = [5, 6, 7, 8, 9, 10 ,11];
hash = arr1.reduce((h,e)=> (h[e]=1, h), {}), //iterate first array once
common = arr2.filter(v=>hash[v]); //iterate secod array once
console.log('Cpmmon elements: ', common);
It searches for itself in sampleArray2, if it finds itself it'll add the number to similarArray
var sampleArray = [1, 2, 3, 4, 5, 6, 7];
var similarArray = [];
for (const num of sampleArray) {
if (sampleArray2.indexOf(num) {
similarArray.push(num);
}
}
var sampleArray = [1, 2, 3, 4, 5, 6, 7];
var sampleArray2 = [5, 6, 7, 8, 9, 10 ,11];
var similarElements =[];
for (let i = 0; i < sampleArray.length; i++) {
for (let j = 0; j < sampleArray2.length; j++) {
if (sampleArray[i] === sampleArray2[j]) {
similarElements.push(sampleArray[i]);
}
}
}
console.log(similarElements);
I have a multidimensional array that returns undefined after the last value of every sub-array. Here is my code:
var bigArray = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[10, 11, 12]
];
for (i = 0; i < bigArray.length; i++) {
for (j = 0; j <= bigArray[i].length; j++) {
console.log(bigArray[i][j]);
}
}
Remove the = part from the condition of the second loop. You tries to access an element out of the range of the array. Also declare your variables with var, let or const - in the case with i and j.
var bigArray = [
[1,2,3],
[4,5,6],
[7,8,9],
[10,11,12]
];
for(var i = 0; i < bigArray.length; i++) {
for(var j = 0; j < bigArray[i].length; j++) {
console.log(bigArray[i][j]);
}
}
In this case, '.length' method will return the total number elements in that array, But array index starts from 0. So if an array contains n elements the array index of the last element will be n-1. And if you are trying to access an array with index n it will return an ArrayIndexOutOfBound Exception.
You can try this code
var bigArray = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[10, 11, 12]
];
for (i = 0; i <= bigArray.length-1; i++) {
for (j = 0; j <= bigArray[i].length-1; j++) {
console.log(bigArray[i][j]);
}
}
What is a fast way to find combinations that aren't present in an array yet?
E.g, I have list of points: [1, 2, 4, 9]
And I have a list of connections [[1,2], [1,4], [1,9], [2,4], [4,9]]
So the missing connection in this list is [2,9]. As there is one requirement: every integer must be connected to a bigger integer.
var points = [1, 2, 4, 9];
var connections = [[1,2], [1,4], [1,9], [2,4], [4,9]];
var missing = [];
for(i = 0; i < points.length; i++){
for(j = i + 1; j < points.length; j++){
var found = false;
for(var a = 0; a < connections.length; a++){
if(connections[a][0] == points[i] && connections[a][1] == points[j]){
found = true;
break;
}
}
if(!found) missing.push([points[i], points[j]]);
}
}
console.log(missing);
The above code works, but the amount of for loops makes me think it is reasonably slow. Is there any faster way to do this? View jsfiddle
By sorting the array, you can do it with 2 nests. Sorting takes O(n log n), and the loops are basically O(n ^ 2).
var points = [1, 2, 4, 9];
var connections = [
[1, 2],
[1, 4],
[1, 9],
[2, 4],
[4, 9]
];
connections.sort();
var missing = [];
var currentIndex = 0;
for (var i = 0; i < points.length; i++) {
for (var j = i + 1; j < points.length; j++) {
if (connections[currentIndex][0] == points[i] && connections[currentIndex][1] == points[j]) {
currentIndex++;
} else {
missing.push([points[i], points[j]]);
}
}
}
console.log(missing);
You can use .reduce method in order to generate all the combination of two elements.Then the only thing that will remain is to get the difference from two arrays.
For this, you can use filter method which accepts a callback method.
var points = [1, 2, 4, 9];
points=points.sort();
var connections = [[1,2], [1,4], [1,9], [2,4], [4,9]];
var combinations = points.reduce(function(arr,elem,i){
for(j=i+1;j<points.length;j++)
arr.push([elem,points[j]]);
return arr;
},[]);
var diff=combinations.filter(function(elem,i){
return connections.find(a=>a[0]==elem[0] && a[1]==elem[1])==undefined;
});
console.log(diff);
You could iterate only the outer loop until length - 2 and use a hash table for inserted connections. The sort order of connections does not matter.
var points = [1, 2, 4, 9],
connections = [[1, 2], [1, 4], [1, 9], [2, 4], [4, 9]],
missing = [],
i, j,
pair,
connected = Object.create(null);
connections.forEach(function (a) {
connected[a.join()] = true;
});
for (i = 0; i < points.length - 1; i++) {
for (j = i + 1; j < points.length; j++) {
pair = [points[i], points[j]];
connected[pair.join()] || missing.push(pair);
}
}
console.log(missing);
.as-console-wrapper { max-height: 100% !important; top: 0; }