簡體   English   中英

查找對象和數組中的嵌套值之間的匹配

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

我有兩個數組:

首先,我有一個包含人員信息的對象數組,以及一個包含他們從類別列表中所做的選擇的數組。

在第二個中,我有一個對象數組,其中包含第一個數組中某人可以從中進行選擇的類別。 在每個類別對象中都有一個類別標題和一個selections數組,我希望包含選擇該類別的玩家的列表。

我需要根據選擇每個類別的人數在UI中呈現元素。 我想知道是否有可能對第一個具有類別標題的數組中的人員選擇進行遍歷,在第二個數組中找到匹配的類別標題,並將該人員信息從第一個數組推入到數組中的選擇數組中第二個。

我絕對願意重組這些數組,但是第二個數組包含更多信息,與該問題無關,並且希望保留原樣。 我必須對第一個數組進行硬編碼,因為我只是從發送給我的電子郵件中獲取信息,因此我目前無法在選擇人員時將人員選擇推入第二個數組。

下面是我要實現的示例。

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

這是一種簡單的方法,請閱讀評論以了解

 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) 

如果可以簡化第一個對象,我建議您執行以下操作:

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/

希望這個幫助:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM