Skip to content

Instantly share code, notes, and snippets.

@mrleblanc101
Last active April 12, 2024 18:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrleblanc101/74f38fa57ba379bc1058811ca0cd3cb2 to your computer and use it in GitHub Desktop.
Save mrleblanc101/74f38fa57ba379bc1058811ca0cd3cb2 to your computer and use it in GitHub Desktop.

Columnize

Split an array into equal with objects

const columnize = (array, column, columns) => {
    const total = array.length;
    const perCol = Math.ceil(total / columns);
    let start = perCol * (column - 1);
    const end = perCol * column;
    return array.slice(start, end);
};

Usage:

<div class="mt-6 flex flex-wrap gap-2">
    <div class="flex flex-col gap-2">
        <CourseInformation
            v-for="course in columnize(organization.courses, 1, 2)"
        />
    </div>
    <div class="flex flex-col gap-2">
        <CourseInformation
            v-for="course in columnize(organization.courses, 2, 2)"
        />
    </div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment