Skip to content

This library used to sort an array to a value based tree.

License

Notifications You must be signed in to change notification settings

hank619/treeify-array

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

treeify-array

Description

This library is used to sort an array to a value based tree.

Usage

yarn add treeify-array

or

npm install treeify-array

Example

import { treeify } from 'treeify-array';

const arr = [
    { name: 'Alice', grade: 'grade-1', class: 'class-1' },
    { name: 'Bob', grade: 'grade-1', class: 'class-2' },
    { name: 'Charlie', grade: 'grade-2', class: 'class-3' },
    { name: 'David', grade: 'grade-2', class: 'class-4' },
  ];

const tree = treeify(arr, ['grade', 'class']);
console.log(tree);
// {
//     'grade-1': {
//       'class-1': [
//         {
//           name: 'Alice',
//           grade: 'grade-1',
//           class: 'class-1',
//         },
//       ],
//       'class-2': [
//         {
//           name: 'Bob',
//           grade: 'grade-1',
//           class: 'class-2',
//         },
//       ],
//     },
//     'grade-2': {
//       'class-3': [
//         {
//           name: 'Charlie',
//           grade: 'grade-2',
//           class: 'class-3',
//         },
//       ],
//       'class-4': [
//         {
//           name: 'David',
//           grade: 'grade-2',
//           class: 'class-4',
//         },
//       ],
//     },
//   }

About

This library used to sort an array to a value based tree.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published