如何过滤对象的javascript数组 [英] how to filtering javascript array of objects

查看:52
本文介绍了如何过滤对象的javascript数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数组.我正在使用PubSidebar过滤基于groupKey的内容.

I have got two arrays . I am filtering based groupKey with PubSidebar.

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的内容数组中传递值.

I am facing problem to pass objects inside content array if parents value: Journals or Deposits or any value or role:pubic. I have to pass value inside content array based on groupKey.

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

If Journals and Deposits is existed , then adding Journals and Deposit data inside content array, including with public data . (three Objects)

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

If Journals is existed , then adding Journals data inside content array including with public data(two Objects) If Deposits is existed , then adding Deposits data inside content array including with public data(two Objects)

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

if GroupKey journals is matched with content object in pubsidebar then two objects , we will get

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

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

if GroupKey Deposits is matched with content object in pubsidebar then two objects

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

如果GroupKey日记帐和存款与pubsidebar中的content对象匹配,则三个对象,

if GroupKey Journals and Deposits is matched with content object in pubsidebar then three objects ,

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

推荐答案

如果我理解正确,则希望过滤PubSidebar.如果值具有公共角色或groupKey中包含的值,则保留这些值.如果是这样,这将是您的功能:

If I understand correctly you want to filter PubSidebar. Keeping values if they have a role of public or a value of whats included in the groupKey. If so this would be your function:

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

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

If you want to run that on content as well we could pull it apart:

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);

这篇关于如何过滤对象的javascript数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆