首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尝试编写一个函数来确定复杂对象的深度嵌套的子数组是否具有值

尝试编写一个函数来确定复杂对象的深度嵌套的子数组是否具有值
EN

Stack Overflow用户
提问于 2020-05-05 05:59:41
回答 2查看 48关注 0票数 2

我正在尝试编写一个函数来检查复杂对象中的一个深度嵌套的子数组,如果它有值,则返回true,如果它为空,则返回false,但我不确定如何做。

我尝试检查的部分是每个contactGroups部分中的联系人,这是我遇到问题的地方,因为数组像object > array of objects > object > contacts array一样嵌套在4层下,我不确定如何在这一层上映射或迭代。

这是我第一次通过一个函数,在这一点上它更像是伪代码:

代码语言:javascript
复制
  const hasContacts = (contacts: {}) => {
    if(contacts.contactGroups.length === 0 
       || contacts.contactGroups.map((contact) => contactGroups.contacts === undefined 
       || contacts.contactGroups.map((contact) => contactGroups.contacts.length === 0 ){
      return false
    }

    return contacts
  }

数据结构如下所示:

代码语言:javascript
复制
const mockContacts = {
  count: 1,
  contactGroups: [
    {
      contactGroup: "Family",
      count: 2,
      contacts: [
        {
          member: "Uncle",
          fullName: "BENJAMIN BILLIARDS",
          lastName: "BILLIARDS",
          firstName: "BENJAMIN",
          email: "shark@billiards.com",
        },
        {
          member: "Aunt",
          fullName: "DENISE NICE",
          lastName: "NICE",
          firstName: "DENISE",
          email: "floral@flowers.com",
        }
      ]
    },
    {
      contactGroup: "Friends",
      count: 2,
      contacts: [
        {
          member: "School Friend",
          fullName: "DERRICK SMITH",
          lastName: "SMITH",
          firstName: "DERRICK",
          email: "smith978@gmail.com",
        },
        {
          member: "Work Friend",
          fullName: "TARA SKY",
          lastName: "SKY",
          firstName: "TARA",
          email: "uptown94@gmail.com",
        }
      ]
    }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-05-05 07:00:53

如果您希望返回布尔值(如果有任何联系人),则可以执行以下操作:

代码语言:javascript
复制
const hasContacts = ({ contactGroups = [] } = []) =>
  contactGroups.some(
    ({ contacts = [] } = {}) => contacts.length
  );

console.log('pass undefined', hasContacts());
console.log('pass empty object', hasContacts({}));
console.log(
  'pass empty contact group',
  hasContacts({ contactGroups: [] })
);
console.log(
  'pass empty contacts',
  hasContacts({ contactGroups: [{ contacts: [] }] })
);
console.log(
  'pass contacts',
  hasContacts({ contactGroups: [{ contacts: [1] }] })
);
console.log(
  'pass some contacts',
  hasContacts({
    contactGroups: [{ contacts: [] }, { contacts: [1] }],
  })
);

const mockContacts = {
  count: 1,
  contactGroups: [
    {
      contactGroup: 'Family',
      count: 2,
      contacts: [
        {
          member: 'Uncle',
          fullName: 'BENJAMIN BILLIARDS',
          lastName: 'BILLIARDS',
          firstName: 'BENJAMIN',
          email: 'shark@billiards.com',
        },
        {
          member: 'Aunt',
          fullName: 'DENISE NICE',
          lastName: 'NICE',
          firstName: 'DENISE',
          email: 'floral@flowers.com',
        },
      ],
    },
    {
      contactGroup: 'Friends',
      count: 2,
      contacts: [
        {
          member: 'School Friend',
          fullName: 'DERRICK SMITH',
          lastName: 'SMITH',
          firstName: 'DERRICK',
          email: 'smith978@gmail.com',
        },
        {
          member: 'Work Friend',
          fullName: 'TARA SKY',
          lastName: 'SKY',
          firstName: 'TARA',
          email: 'uptown94@gmail.com',
        },
      ],
    },
  ],
};
console.log(
  'mock contacts:',
  hasContacts(mockContacts)
);

票数 2
EN

Stack Overflow用户

发布于 2020-05-05 06:19:44

假设嵌套的contactGroup联系人不能有更多的嵌套,那么这个解决方案应该适用于您。我不清楚您希望如何处理每个嵌套组,所以我返回了一个数组,该数组将告诉您每个嵌套组是否有联系人。

代码语言:javascript
复制
const mockContacts = {
    count: 1,
    contactGroups: [
        {
            contactGroup: "Family",
            count: 2,
            contacts: [
                {
                    member: "Uncle",
                    fullName: "BENJAMIN BILLIARDS",
                    lastName: "BILLIARDS",
                    firstName: "BENJAMIN",
                    email: "shark@billiards.com",
                },
                {
                    member: "Aunt",
                    fullName: "DENISE NICE",
                    lastName: "NICE",
                    firstName: "DENISE",
                    email: "floral@flowers.com",
                }
            ]
        },
        {
            contactGroup: "Friends",
            count: 2,
            contacts: [
                {
                    member: "School Friend",
                    fullName: "DERRICK SMITH",
                    lastName: "SMITH",
                    firstName: "DERRICK",
                    email: "smith978@gmail.com",
                },
                {
                    member: "Work Friend",
                    fullName: "TARA SKY",
                    lastName: "SKY",
                    firstName: "TARA",
                    email: "uptown94@gmail.com",
                }
            ]
        }
    ]
}

const hasContacts = (contacts) => {
    // if contacts is not undefined, then check contactGroup
    if (contacts && Array.isArray(contacts.contactGroups)) {
        // we can have more than one contact group so need to check each
        return contacts.contactGroups.map(row=>Array.isArray(row.contacts) && row.contacts.length > 0)
    }
}

console.log(hasContacts(mockContacts))

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61602359

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档