Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I do recursion in an array and make the isShow flag true? #1918

Open
fr0ntsmaverick opened this issue Dec 28, 2023 · 0 comments
Open

Comments

@fr0ntsmaverick
Copy link

I have an array nested within an array. What is the correct way to write a recursive function? I'm just new to using the library fp-ts

I wrote a function. Which makes the flag isShow true.

Link to example https://stackblitz.com/edit/typescript-39acrw?file=index.ts


import { pipe } from 'fp-ts/function';
import * as A from 'fp-ts/Array';
import * as S from 'fp-ts/Separated';

interface Ichildren {
  isActive: boolean;
  name: string;
  key: string;
}

interface IMenu {
  id: number;
  title: string;
  children: Ichildren[];
}

interface IgetSidebar {
  menu: IMenu[];
  isAbility: boolean;
}

const menus: IMenu[] = [
  {
    title: 'title 1',
    id: 1,
    children: [
      {
        name: 'children 2',
        key: 'children1',
        isActive: true,
      },
    ],
  },
  {
    title: 'title 2',
    id: 2,
    children: [
      {
        name: 'children 21',
        key: 'children21',
        isActive: true,
      },
      {
        name: 'children 22',
        key: 'children22',
        isActive: false,
      },
    ],
  },
  {
    title: 'title 3',
    id: 2,
    children: [
      {
        name: 'children 3',
        key: 'children3',
        isActive: true,
      },
    ],
  },
];

const getSidebar = ({ menu, isAbility }: IgetSidebar) =>
  pipe(
    menu,
    A.map((el) => ({
      ...el,
      children: pipe(
        el.children,
        A.partition((children) => {
          if (children.key === 'children22') {
            return (children.isActive = isAbility);
          }

          return children.isActive;
        }),
        S.right
      ),
    }))
  );

console.log(
  getSidebar({
    menu: menus,
    isAbility: true,
  })
);


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant