简体   繁体   English

查找对象和数组中的嵌套值之间的匹配

[英]Finding a match between nested values in objects and arrays

I have two arrays: 我有两个数组:

In the first, I have an array of objects that contain a persons information, and an array which contains selections they have made from a list of categories. 首先,我有一个包含人员信息的对象数组,以及一个包含他们从类别列表中所做的选择的数组。

In the second, I have an array of objects that contain the categories a person from the first array could have made selections from. 在第二个中,我有一个对象数组,其中包含第一个数组中某人可以从中进行选择的类别。 Inside each of the category objects there is a category title, and a selections array which I would like to contain a list of the players who have selected this category. 在每个类别对象中都有一个类别标题和一个selections数组,我希望包含选择该类别的玩家的列表。

I need to render elements in my ui based on the number of people who have selected a category, for every category. 我需要根据选择每个类别的人数在UI中呈现元素。 I am wondering if it is possible for me to iterate through a persons selections from the first array, which has the category title, find the matching category title in the second array and push that persons information from the first array into the selections array in the second one. 我想知道是否有可能对第一个具有类别标题的数组中的人员选择进行遍历,在第二个数组中找到匹配的类别标题,并将该人员信息从第一个数组推入到数组中的选择数组中第二个。

I am definitely open to restructuring these arrays, but the second one has a lot more information, not relevant to this problem, and would prefer to leave as is. 我绝对愿意重组这些数组,但是第二个数组包含更多信息,与该问题无关,并且希望保留原样。 I have to hardcode the first array, as I am just getting that information from emails being sent to me, I currently have no way of pushing a persons selections into the second array at the time they are being chosen. 我必须对第一个数组进行硬编码,因为我只是从发送给我的电子邮件中获取信息,因此我目前无法在选择人员时将人员选择推入第二个数组。

An example of what I'm trying to achieve is below. 下面是我要实现的示例。

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',
                },
            ]
        },
    ]

Here is a simple way of doing it, read the comment to understand 这是一种简单的方法,请阅读评论以了解

 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) 

If you can simplify the first object I would recommend you to do something like this: 如果可以简化第一个对象,我建议您执行以下操作:

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

Where you have directly what people have selected. 人们直接选择的地方在哪里。

Then pushing a person's selections into the second array: 然后将一个人的选择推入第二个数组:

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

Here you have a working example (open the console to see the result): https://jsfiddle.net/1pxhvo5k/ 在这里,您有一个有效的示例(打开控制台以查看结果): https : //jsfiddle.net/1pxhvo5k/

Hope this help :) 希望这个帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM