Skip to content

Instantly share code, notes, and snippets.

@DevGW
Created December 29, 2022 13:44
Show Gist options
  • Save DevGW/ffdf85479aeecf7f45c05ea05986106b to your computer and use it in GitHub Desktop.
Save DevGW/ffdf85479aeecf7f45c05ea05986106b to your computer and use it in GitHub Desktop.
Javascript :: Arrays II: NESTED ARRAYS
// Arrays within arrays: NESTED ARRAYS / MULTIDIMENSIONAL ARRAYS
let relatedThings = [['Windows', 'MacOS'], ['New Year', 'Christmas']]
// to access 'New Year' you need to:
console.log(relatedThings[1][0]);
// using nested arrays to represent grids:
let grid = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
console.log(grid[2]); ===> [7, 8, 9]
console.log(grid[0][1]); ===> 2
// making a grid
function makeGrid(numColumns, numRows) {
let grid = [];
for (let i=0; i < numRows; i++) {
let row = [];
for (let j=1; j <= numColumns; j++) {
row.push(j);
}
grid.push(row);
}
console.log(grid);
return grid;
}
// to extract a column:
let secondCol = [];
for (let i=0; i < grid.length; i++) {
secondCol.push(grid[i][1]);
}
console.log(secondCol);
// extracts even elements into new array and puts both arrays back together
function evenAndOdd(anArr) {
let evenArray = [];
let newArray = [];
let currElem = '';
for (let i=0; i < anArr.length; i++) {
currElem = anArr[i];
if (currElem % 2 === 0) {
evenArray.push(currElem);
anArr.splice(i, 1);
}
}
newArray.push(evenArray);
newArray.push(anArr);
return newArray;
}
// same as above refactored
function evenAndOdd(originalArray) {
let evenNums = [];
let oddNums = [];
for (let i = 0; i <= originalArray.length; i++) {
let num = originalArray[i];
if (num % 2) {
oddNums.push(num);
}
else {
evenNums.push(num);
}
}
}
// extract nested array into a flat array
function arrayFlattener(anArr) {
let flatArr = [];
for (let i=0; i < anArr.length; i++) {
let element = anArr[i];
if (Array.isArray(element)) {
for (let j = 0; j < element.length; j++) {
let flatElem = element[j];
flatArr.push(flatElem);
}
}
else flatArr.push(anArr[i]);
}
return flatArr;
}
// using .slice
function removeColumns(aGrid, numColums) {
let slimArray = [];
for (let i = 0; i < aGrid.length; i++) {
let currentRow = aGrid[i];
slimArray.push(currentRow.slice(0, currentRow.length - numColums));
}
return slimArray;
}
// join without using .join
function myJoin(anArray, separator = ',') {
let joinedString = '';
for (let i = 0; i < anArray.length -1; ++i) {
const currentElem = anArray[i];
if (currentElem === undefined || currentElem === null) {
joinedString += separator;
} else {
joinedString += `${currentElem}${separator}`;
}
}
if (anArray.length) {
joinedString += anArray[anArray.length -1];
}
return joinedString;
}
// slice without using .slice
function mySlice(anArr, startIdx = 0, endIdx = anArr.length) {
const copyArr = [];
if (startIdx < 0) startIdx = anArr.length + startIdx;
if (endIdx < 0) endIdx = anArr.length + endIdx;
for (let i = startIdx; i < endIdx; ++i) {
const currentElem = anArr[i];
copyArr.push(currentElem)
}
return copyArr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment