Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: add array questions
  • Loading branch information
sohamsshah committed May 28, 2022
commit aca9daac787d4f1110c7920213820ba8dea9c521
27 changes: 27 additions & 0 deletions Snippets/Arrays/flattenADeeplyNestedArray.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
function flattenArray(arr, n = Infinity) {
let res = [];
for (i in arr) {
if (n !== 0 && Array.isArray(arr[i])) {
res.push(...flattenArray(arr[i], n - 1));
} else {
res.push(arr[i]);
}
}
return res;
}
let input = [
1,
2,
3,
[4],
[5, 6, [7], [8, [9, [10]]]],
11,
12,
13,
[14, [[[[[15, [16]]]]]]],
17,
18,
[19, [20, [21, [22, [23, [24, [[[[[25]]]]]]]]]]],
];

console.log(flattenArray(input, 2));
125 changes: 125 additions & 0 deletions Snippets/Arrays/groupBy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
const arr = [6.1, 2.4, 2.7, 6.8];

function groupBy(arr, property) {
const groupByString = (arr, query) => {
let res = {};

arr.forEach((item, index) => {
const queryList = query.split(".");

let currKey;
let currItem = item;
let count = 0;
for (let i = 0; i < queryList.length; i++) {
currKey = queryList[i];
if (!currItem[currKey]) {
break;
} else {
count += 1;
currItem = currItem[currKey];
}
}

if (count !== queryList.length) {
currItem = undefined;
}
if (!res[currItem]) {
res[currItem] = [item];
} else {
res[currItem].push(item);
}
});
return res;
};
const groupByCallback = (arr, cb) => {
let res = {};
arr.forEach((item) => {
const ans = cb.call(null, item);

if (Object.keys(res).includes(ans.toString())) {
res[ans].push(item);
} else {
res[ans] = [item];
}
});
return res;
};

let res;
switch (typeof property) {
case "function":
res = groupByCallback(arr, property);
break;
case "string":
res = groupByString(arr, property);
break;
default:
return new Error("Invalid Property Type");
}

return res;
}

// groupBy(arr, Math.floor);
console.log(groupBy(arr, Math.ceil));
console.log(groupBy([6.1, 4.2, 6.3], Math.floor));
console.log(groupBy(["one", "two", "three"], "length"));
console.log(
groupBy(
[{ a: { b: { c: 1 } } }, { a: { b: { c: 1 } } }, { a: { b: { c: 2 } } }],
"b.a.c"
)
);

console.log(
groupBy(
[{ a: { b: { c: 1 } } }, { a: { b: { c: 1 } } }, { a: { b: { c: 2 } } }],
"a.b.c"
)
);

// alternate solution
function groupByAlternative(collection, property) {
const output = {};

if (!collection || typeof collection !== "object") {
return output;
}

const isPropertyFunction = typeof property === "function";
const isPropertyPath = typeof property === "string";

for (const value of Object.values(collection)) {
let current = undefined;

if (isPropertyFunction) {
current = property(value);
} else if (isPropertyPath) {
// a.b.c -> [a, b, c];
const path = property.split(".");
let i;
let currentKey;
let currentItem = value; // { a: { b: { c: 1 } } }

for (i = 0; i < path.length; i++) {
// [a, b, c] -> currentKey: path[0] -> a
// [a, b, c] -> currentKey: path[1] -> b
// [a, b, c] -> currentKey: path[2] -> c
currentKey = path[i];

if (!currentItem[currentKey]) {
currentItem = undefined;
break;
}
currentItem = currentItem[currentKey];
}

current = currentItem;
}

output[current] = output[current] || [];
output[current].push(value);
}

return output;
}