【问题标题】:Finding a match between nested values in objects and arrays查找对象和数组中的嵌套值之间的匹配
【发布时间】:2019-02-22 08:22:27
【问题描述】:

我有两个数组:

首先,我有一个包含人员信息的对象数组,以及一个包含他们从类别列表中选择的数组。

在第二个中,我有一个对象数组,其中包含第一个数组中的人可以从中进行选择的类别。在每个类别对象中都有一个类别标题和一个 selections 数组,我想在其中包含选择该类别的玩家列表。

我需要根据每个类别选择类别的人数在我的用户界面中呈现元素。我想知道我是否可以遍历第一个数组中的人员选择,该数组具有类别标题,在第二个数组中找到匹配的类别标题并将该人员信息从第一个数组推送到选择数组中第二个。

我绝对愿意重组这些数组,但第二个包含更多信息,与这个问题无关,并且更愿意保持原样。我必须对第一个数组进行硬编码,因为我只是从发送给我的电子邮件中获取该信息,我目前无法在选择人员时将其推送到第二个数组中。

下面是我想要实现的一个示例。

listOfPeople: [
        {
            personName: 'Eric Smith',
            selections: [
                {
                    categoryTitle: 'Fruit',
                    categoryItem: 'Apple',
                },
                {
                    categoryTitle: 'Animals',
                    categoryItem: 'Dog',
                },
                {
                    categoryTitle: 'Cars',
                    categoryItem: 'Ford'
                },
            ]
        },
        {
            personName: 'Sarah Edwards',
            selections: [
                {
                    categoryTitle: 'Shoes',
                    categoryItem: 'Running Shoe',
                },
                {
                    categoryTitle: 'Animals',
                    categoryItem: 'Cat',
                },
            ]
        }
    ],
    listOfCategories: [
        {
            categoryTitle: 'Fruit',
            peopleWhoSelected: [
                {
                    personName: 'Eric Smith',
                    categoryItem: 'Apple',
                },
            ]
        },
        {
            categoryTitle: 'Animals',
            peopleWhoSelected: [
                {
                    personName: 'Eric Smith',
                    categoryItem: 'Dog',
                },
                {
                    personName: 'Sarah Edwards',
                    categoryItem: 'Cat',
                },
            ]
        },
        {
            categoryTitle: 'Cars',
            peopleWhoSelected: [
                {
                    personName: 'Eric Smith',
                    categoryItem: 'Ford',
                },
            ]
        },
    ]

【问题讨论】:

  • edit您的问题包括您开始使用的两个数组中的示例数据,以及您为尝试自己解决问题而编写的任何代码。
  • 更正了大写问题。

标签: javascript arrays


【解决方案1】:

如果你可以简化第一个对象,我建议你这样做:

const people =
{
  "Eric Smith": {
    "Fruit": "Apple",
    "Animals": "Dog",
    "Cars": "Ford"
  },
  "Sarah Edwards": {
    "Shoes": "Running Shoe",
    "Animals": "Cat"
  }
}

您可以直接获得人们选择的内容。

然后将一个人的选择推入第二个数组:

Object.entries(people).forEach(([person, categories]) => 
  Object.entries(categories).forEach(([title, item]) => {
    let category = listOfCategories.find(c => c.categoryTitle == title)
    // Create category if we didn't find it
    if (!category) {
      category = {
        categoryTitle: title,
        peopleWhoSelected: []
      }
      listOfCategories.push(category)
    }
    // Add item selected and person name to category
    category.peopleWhoSelected.push({
      personName: person,
      categoryItem: item
    })    
  }))

这里有一个工作示例(打开控制台查看结果):https://jsfiddle.net/1pxhvo5k/

希望有帮助:)

【讨论】:

    【解决方案2】:

    这里有一个简单的做法,看评论就明白了

    var listOfPeople = [{personName: 'Eric Smith'}, {  personName: 'Sarah Edwards'}]
    
    var listOfCategories= [
            {
                categoryTitle: 'Fruit',
                peopleWhoSelected: [
                    {
                        personName: 'Eric Smith',
                        categoryItem: 'Apple',
                    },
                ]
            },
            {
                categoryTitle: 'Animals',
                peopleWhoSelected: [
                    {
                        personName: 'Eric Smith',
                        categoryItem: 'Dog',
                    },
                    {
                        personName: 'Sarah Edwards',
                        categoryItem: 'Cat',
                    },
                ]
            },
            {
                categoryTitle: 'Cars',
                peopleWhoSelected: [
                    {
                        personName: 'Eric Smith',
                        categoryItem: 'Ford',
                    },
                ]
            },
        ]
        
        listOfCategories.forEach((cat)=>{
         cat.peopleWhoSelected.forEach((s)=> {
          // find the right person
          var person = listOfPeople[listOfPeople.findIndex(p=> p.personName == s.personName )];
          // check if there is a property selections, if no then add it
         if (!person.selections)
              person.selections = [];
              // if Person dose not already have this categoryItem then add it
            if (person.selections.findIndex(x=> x.categoryItem == s.categoryItem) ==-1)
                person.selections.push({  categoryTitle: cat.categoryTitle, categoryItem: s.categoryItem });
         
         });
        
        });
        
        
        console.log(listOfPeople)
        

    【讨论】:

      猜你喜欢
      • 2020-07-14
      • 1970-01-01
      • 1970-01-01
      • 2017-04-04
      • 2021-06-13
      • 2018-09-23
      • 1970-01-01
      • 1970-01-01
      • 2013-12-31
      相关资源
      最近更新 更多