小编典典

如何过滤对象的javascript数组

reactjs

我有两个阵列。我正在用PubSidebar过滤基于groupKey。

let groupKey = ['oaDeal', 'Journals', 'Deposit']

   // This array of object will be filtering with groupKey

const PubSidebar = [

  {

    value: 'Dashboard',

    role: 'public',

  },

  {

    value: 'oaDeal',

    role: 'private',

    content: [

      {

        role: 'private',

        value: 'oaDeal',

      },

    ],

  },

  {

    value: 'Journals',

    role: 'public',

    content: [

      {

        role: 'private',

        value: 'Journals',

      },

      {

        role: 'private',

        value: 'Token',

      },

      {

        role: 'private',

        value: 'policy',

      },

      {

        role: 'private',

        value: 'Deposit',

      },

      { role: 'public', value: 'test' },

    ],

  },

]

// Here is my trying code. I am filtering PubSidebar

const res = PubSidebar.filter(x => {

  // when it's main outerloop role public or private and groupKey matched

  if (

    x.role === 'public' ||

    (x.role === 'private' && groupKey.includes(x.value))

  ) {

    // then if inner-array exist then inner array filtering

    if (x.content) {

      // inside content assign condition public or private and groupKey

      let tempX = x.content.filter(

        y =>

          y.role === 'public' ||

          (y.role === 'private' && groupKey.includes(y.value)) ||

          x.value === y.value

      )

      x.content = tempX

      console.log(tempX)

      return x

    } else {

      // Other wise give me a single object public

      console.log(x)

      return x

    }

  }







})

如果父母重视:日记或存款或任何价值或角色:公共,我在传递内容数组内的对象时遇到问题。我必须在基于的内容数组中传递值groupKey

如果存在Journals and Deposits,则在内容数组内添加Journals and Deposit数据,包括公共数据。(三个对象)

如果存在Journals,则将Contents数组内的Journals数据添加到公共数据(两个对象)中如果存在Deposits,则将Contents数组内包含公共数据的Deposits数据添加到两个公共对象中

如果GroupKey日记与pubsidebar中的content对象匹配,则有两个对象,我们将得到

{
    value: 'Journals',
    role: 'public',
    content: [
      {
        role: 'private',
        value: 'Journals',
      },
      { role: 'public', value: 'test' },
    ],
  }

如果GroupKey存款与pubsidebar中的内容对象匹配,则两个对象

{
    value: 'Deposit',
    role: 'public',
    content: [
      {
        role: 'private',
        value: 'Deposit',
      },
      { role: 'public', value: 'test' },
    ],
  }

如果GroupKey Journals和Deposits与pubsidebar中的content对象匹配,则三个对象,

    {
        value: 'Deposit' || "Journals",
        role: 'public',
        content: [
{
            role: 'private',
            value: 'Journals',
          },
          {
            role: 'private',
            value: 'Deposit',
          },
          { role: 'public', value: 'test' },
        ],
      }

阅读 337

收藏
2020-07-22

共1个答案

小编典典

如果我理解正确,则想过滤PubSidebar。如果值具有公共作用或groupKey中包含的值,则保留这些值。如果是这样,这将是您的功能:

PubSidebar.filter(x => (x.role === 'public' || groupKey.includes(x.value));

如果您也想在内容上运行它,我们可以将其拆开:

const filterByGroup = (x) => (x.role === 'public' || groupKey.includes(x.value));

let result = [];
for (let i = 0; i < PubSidebar.length; i++) {
  const item = PubSidebar[i];

  if (filterByGroup(item)) {
    if (item.content) {
      item.content = item.content.filter(filterByGroup);
    }
    result = [ ...result, item ];
  }
}

片段:

let groupKey = ['oaDeal', 'Journals', 'Deposit']

const PubSidebar = [{

    value: 'Dashboard',

    role: 'public',

  },

  {

    value: 'oaDeal',

    role: 'private',

    content: [{

      role: 'private',

      value: 'oaDeal',

    }, ],

  },

  {

    value: 'Journals',

    role: 'public',

    content: [{

        role: 'private',

        value: 'Journals',

      },

      {

        role: 'private',

        value: 'Token',

      },

      {

        role: 'private',

        value: 'policy',

      },

      {

        role: 'private',

        value: 'Deposit',

      },

      {

        role: 'public',

        value: 'test'

      },

    ],

  },

]



const filterByGroup = (x) => (x.role === 'public' || groupKey.includes(x.value));



let result = [];

for (let i = 0; i < PubSidebar.length; i++) {

  const item = PubSidebar[i];



  if (filterByGroup(item)) {

    if (item.content) {

      item.content = item.content.filter(filterByGroup);

    }

    result = [...result, item];

  }

}

console.log(result);
2020-07-22