簡體   English   中英

如何將字典添加到數組或字典鍵?

[英]How to append dictionaries to an array or dictionary key?

我正在做一個將老師映射到班級和班級信息的項目。 我正在使用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
}

實際上,我按名稱順序具有所有班級信息和講師,但我只需要對它們進行分組。 您對使用數組還是字典有任何建議? 另外,是否可以將某個類附加到字典或某個特定教師的數組?

您只需要創建一個對象數組,如下所示:

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

您可以將對象存儲在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(/[AZ]+/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') ); 

在對象數組(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);

暫無
暫無

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

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