我正在做一个将老师映射到班级和班级信息的项目。我正在使用javaScript。
我的字典设置如下:
{ Instructor: 'Gondree, Mark',
Class: 'CS 349',
Section: '001',
Components: [ 'ACT' ],
Title: 'Problem Solving in a Team Envn',
WTU: 1.3,
Units: '1',
Meeting_Days: 'F',
Time: '10:00:00 - 11:50:00',
Room: 'STEV1034',
Enrollment: 22,
FTE: 1.47 }
我有很多这样的老师。我遇到的问题是使用讲师将它们分组为字典或数组。我理想的输出是:
{
MARK GONDREE{
{ Instructor: 'Gondree, Mark',
Class: 'CS 349',
Section: '001',
Components: [ 'ACT' ],
Title: 'Problem Solving in a Team Envn',
WTU: 1.3,
Units: '1',
Meeting_Days: 'F',
Time: '10:00:00 - 11:50:00',
Room: 'STEV1034',
Enrollment: 22,
FTE: 1.47 },
ANOTHER CLASS HERE
GLEN CARTER{
{ Instructor: 'Carter, Glenn',
Class: 'CS 101',
Section: '018',
Components: [ 'DIS', 'ACT' ],
Title: 'Intro Computers & Computing',
WTU: 1.3,
Units: '3',
Meeting_Days: 'W',
Time: '10:00:00 - 11:50:00',
Room: 'DARW0024',
Enrollment: 14,
FTE: 2.8 },
ANOTHER CLASS HERE
}
从本质上讲,我按名称顺序具有所有班级信息和讲师,但我只需要将它们分组。您对使用数组还是字典有任何建议?另外,是否可以将某个类附加到词典或由某个特定教师组成的数组?
答案 0 :(得分:2)
您只需要创建一个像这样的对象数组:
let instructors = [
{ Instructor: 'Gondree, Mark', Class: 'CS 349', Section: '001', Components: [ 'ACT' ], Title: 'Problem Solving in a Team Envn', WTU: 1.3, Units: '1', Meeting_Days: 'F', Time: '10:00:00 - 11:50:00', Room: 'STEV1034', Enrollment: 22, FTE: 1.47 },
{ Instructor: 'Carter, Glenn', Class: 'CS 101', Section: '018', Components: [ 'DIS', 'ACT' ], Title: 'Intro Computers & Computing', WTU: 1.3, Units: '3', Meeting_Days: 'W', Time: '10:00:00 - 11:50:00', Room: 'DARW0024', Enrollment: 14, FTE: 2.8 },
{ Instructor: 'Gondree, Mark', Class: 'CS 349', Section: '001', Components: [ 'ACT' ], Title: 'Problem Solving in a Team Envn', WTU: 1.3, Units: '1', Meeting_Days: 'F', Time: '10:00:00 - 11:50:00', Room: 'STEV1034', Enrollment: 22, FTE: 1.47 }
];
console.log(instructors[0].Instructor);
> Gondree, Mark
答案 1 :(得分:1)
您可以将对象存储在Map
中,并使用Map.get(<instructor name>)
获取相应的对象,并使用Map.set()
设置或修改对象。
const data = [{"Instructor":"Gondree, Mark","Class":"CS 349","Section":"001","Components":["ACT"],"Title":"Problem Solving in a Team Envn","WTU":1.3,"Units":"1","Meeting_Days":"F","Time":"10:00:00 - 11:50:00","Room":"STEV1034","Enrollment":22,"FTE":1.47}];
const formatEntry = entry =>
[
entry.Instructor // get `Instructor` property value
.toUpperCase() // convert to uppercase
.match(/[A-Z]+/ig) // match only letters, omitting space and comma
.reverse() // reverse original entry format of name `'Gondree, Mark'`
.join(' ') // join name array with space character
, entry // original plain object `entry`
];
const instructors = new Map(data.map(formatEntry)); // enter initial entry
// set new entry
instructors.set(
...formatEntry({"Instructor":"Carter, Glenn","Class":"CS 101","Section":"018","Components":["DIS","ACT"],"Title":"Intro Computers & Computing","WTU":1.3,"Units":"3","Meeting_Days":"W","Time":"10:00:00 - 11:50:00","Room":"DARW0024","Enrollment":14,"FTE":2.8})
);
// get entries
console.log(
instructors.get('MARK GONDREE')
, instructors.get('GLENN CARTER')
);
答案 2 :(得分:1)
在对象的数组(arr代表原始数组)上使用映射,并在删除“ x.Instructor”后创建新的对象数组。
let new_arr = arr.map(x => ({
[x.Instructor]: (() => {
delete x.Instructor;
return x;
})()
}))
console.log('arr :', arr);
console.log('new_arr :', new_arr);