我坚持一件事。我检查了现有问题,但没有找到好的解决方案。我需要从3个对象创建这样的数组。
var object1 = {
1: { name: 'name1' },
2: { name: 'name2' },
3: { name: 'name3' },
};
var object2 = {
1: [1,2,3],
2: [4,5,6],
3: [7,8,9],
};
var object3 = {
1: { sport: 'football' },
2: { sport: 'hockey' },
3: { sport: 'snooker' },
4: { sport: 'walleyball' },
5: { sport: 'moto' },
6: { sport: 'f1' },
7: { sport: 'boxing' },
8: { sport: 'karate' },
9: { sport: 'ufc' },
};
最后,我需要获取具有该视图的对象的数组。
var array = [
{
name: 'name1',
sports: [
{ sport: 'football' },
{ sport: 'hockey'},
{ sport: 'snooker' }
]
},
{
name: 'name2',
sports: [
{ sport: 'walleyball' },
{ sport: 'moto'},
{ sport: 'f1' }
]
},
{
name: 'name3',
sports: [
{ sport: 'boxing' },
{ sport: 'karate'},
{ sport: 'ufc' }
]
},
];
Object1
主要类别,Object2
运动选择,Object3
所有现有运动
我的尝试:
const categoriesWithSports = {};
const categorySportsNew = { ...object2 };
Object.keys(categorySportsNew).forEach(sportsId => {
const currentCategoryArray = object2[sportsId];
const sportsWithCategoriesArray = currentCategoryArray.map(sportNewId =>
{
Object.keys(object3).forEach(sportsKey => {
const currentSport = sportsId[sportsKey];
return currentSport;
});
categorySportsNew[sportsId] = sportsWithCategoriesArray;
});
});
那之后,我得到无限循环。
答案 0 :(得分:1)
仅遍历对象并进行正确的连接。我已将对象重命名以提高代码的可读性。
const people = {
1: { name: 'name1' },
2: { name: 'name2' },
3: { name: 'name3' },
};
const sportsoptions = {
1: [1,2,3],
2: [4,5,6],
3: [7,8,9],
};
const sportsmap = {
1: { sport: 'football' },
2: { sport: 'hockey' },
3: { sport: 'snooker' },
4: { sport: 'walleyball' },
5: { sport: 'moto' },
6: { sport: 'f1' },
7: { sport: 'boxing' },
8: { sport: 'karate' },
9: { sport: 'ufc' },
};
const result = [];
for (const prop in people) {
let person = { name: people[prop].name, sport: []};
for (let sportsoption of sportsoptions[prop]) {
person.sport.push({sport: sportsmap[sportsoption].sport})
}
result.push(person);
}
console.log(result);
答案 1 :(得分:0)
您可以使用正常的for循环进行
var object1 = {
1: { name: 'name1' },
2: { name: 'name2' },
3: { name: 'name3' },
};
var object2 = {
1: [1,2,3],
2: [4,5,6],
3: [7,8,9],
};
var object3 = {
1: { sport: 'football' },
2: { sport: 'hockey' },
3: { sport: 'snooker' },
4: { sport: 'walleyball' },
5: { sport: 'moto' },
6: { sport: 'f1' },
7: { sport: 'boxing' },
8: { sport: 'karate' },
9: { sport: 'ufc' },
};
var res = [];
for(var key in object1) {
let val = object1[key];
let obj = {};
obj['name'] = val.name;
obj.sports = [];
let secondVal = object2[key];
secondVal.forEach(val => {
let newObj = {};
newObj['sport '] = object3[val]['sport'];
obj.sports.push(newObj);
})
res.push(obj)
}
console.log(res)
答案 2 :(得分:0)
在map()
内包含map()
,非常干净。这只是令人困惑,因为原始数据很尴尬,可能应该是数组而不是对象。
var object1 = {1: { name: 'name1' },2: { name: 'name2' },3: { name: 'name3' },};
var object2 = {1: [1,2,3],2: [4,5,6],3: [7,8,9],};
var object3 = {1: { sport: 'football' },2: { sport: 'hockey' },3: { sport: 'snooker' },4: { sport: 'walleyball' },5: { sport: 'moto' },6: { sport: 'f1' },7: { sport: 'boxing' },8: { sport: 'karate' },9: { sport: 'ufc' },};
let res = Object.entries(object1).map(([k, v]) => {
v.sports = object2[k].map(i => ({sport: object3[i].sport}))
return v
})
console.log(res)