[英]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.