Skip to content

Instantly share code, notes, and snippets.

View chanonroy's full-sized avatar
🚀

Chanon Roy chanonroy

🚀
View GitHub Profile
const nestedData = [1, 2, [3, [4, 5]]]
const flatten = (arr) => {
return arr.reduce((acc, cur) => {
return acc.concat(Array.isArray(cur) ? flatten(cur) : cur)
}, [])
}
const result = flatten(nestedData)
const categories = [
{ id: 1, label: "Action" },
{ id: 2, label: "Romance" },
{ id: 3, label: "Comedy" },
{ id: 4, label: "Horror" },
]
const result = categories.reduce((acc, cur) => {
const { id, label } = cur
acc[id] = label
const data = [
{ name: "Chanon", age: 28, voted: true },
{ name: "Tim", age: 20, voted: true },
{ name: "Sarah", age: 27, voted: true },
{ name: "Jeff", age: 17, voted: false },
]
const result = data.reduce((acc, cur) => {
return {
maxAge: Math.max(acc.maxAge, cur.age),
const data = [
{ name: "Chanon", age: 28, voted: true },
{ name: "Tim", age: 20, voted: true },
{ name: "Sarah", age: 27, voted: true },
{ name: "Jeff", age: 17, voted: false },
]
const result = data.reduce((acc, cur) => {
return {
totalVotes: acc.totalVotes + (cur.voted ? 1 : 0)
const data = [1, 2, 3]
const result = data.reduce((acc, cur) => {
return acc + cur
}, 0)
// result: 6
import { debounce } from 'lodash'
import { track } from './analytics-service'
export default function App() {
const [value, setValue] = useState("")
const trackAnalytics = (val) => {
if (val) track(val)
}
import { debounce } from 'lodash'
import { track } from './analytics-service'
export default function App() {
const [value, setValue] = useState("")
const trackAnalytics = (val) => {
if (val) track(val)
}
export const waitFor = async (cb: any) => {
let c = 0
async function f() {
try {
await cb()
} catch (error) {
if (c < 9) {
c += 1
setTimeout(f, 500)