1

Is it possible to get an output array from the given that contains roles=1 without duplicate ? Iam using angular 6 typescript. Is there any typescript array processing functions to do this operation //Input Array

export const userMenus = [
{
    name: 'Dashboard',
    url: '/dashboards',
    icon: 'icon-speedometer',
    roles:'1,3,4'
},
  {
    name: 'Users',
    url: '/Users',
    icon: 'icon-bell',
    roles:'1,2,3,4'
  },
  {
    name: 'Article',
    url: '/Users',
    icon: 'icon-bell',
    roles:'1,2,3,4',
    children: [
             {
              name: 'Cards',
               url: '/base/cards',
               icon: 'icon-puzzle',
               roles:'1,3,4',
             },
             {
               name: 'Carousels',
               url: '/base/carousels',
               icon: 'icon-puzzle',
               roles:'2,4',
             },
             {
               name: 'Collapses',
               url: '/base/collapses',
               icon: 'icon-puzzle',
               roles:'4'
             }
            ]      
  }

]

--Need Output if role is 2. removed items that not contain 2 in the role field

userMenus = [
  {
    name: 'Users',
    url: '/Users',
    icon: 'icon-bell',
    roles:'1,2,3,4'
  },
  {
    name: 'Article',
    url: '/Users',
    icon: 'icon-bell',
    roles:'1,2,3,4',
    children: [
             {
               name: 'Carousels',
               url: '/base/carousels',
               icon: 'icon-puzzle',
               roles:'2,4',
             },
            ]      
  }
3
  • have you try .filter(role === '1') on your array ? Commented Dec 12, 2018 at 10:38
  • const filteredUserMenus = userMenus.filter((userMenu) => { return userMenu.roles === '2'; }); this worked , but my array has field comes like roles:'1,2' so cant find in it :( Commented Dec 12, 2018 at 12:21
  • So just make roles and array instead of a string... Commented Dec 12, 2018 at 12:33

1 Answer 1

2

You must filter your array and verify that you have 2 in your roles :

const filteredUserMenus = userMenus.filter((userMenu) => {
  return userMenu.roles.find((role) => role === '2');
});

short syntax :

const filteredUserMenus = userMenus.filter((userMenu) => 
  userMenu.roles.find((role) => role === '2'));

EDIT : your data structure is bad, roles shouldn't be a string but an array of role. Anyway, if you can't change it, here is a solution :

const filteredUserMenus = userMenus.filter((userMenu) => {
  return userMenu.roles.split(',').find((role) => role === '2');
});

short syntax :

const filteredUserMenus = userMenus.filter((userMenu) =>
   userMenu.roles.split(',').find((role) => role === '2'));
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for the response Florian, const filteredUserMenus = userMenus.filter((userMenu) => { return userMenu.roles.find(2); }); will get error -- [ts] Property 'find' does not exist on type 'string'. const filteredUserMenus = userMenus.filter((userMenu) => { return userMenu.roles === '2'; }); this worked , but my array has field comes like roles:'1,2' so cant find in it :(
@Vishnudas : i have updated the answer. Please note that your data structure shouldn't be a string, but an array of string. If you can, you should change the structure, else, use the edited solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.