gpt4 book ai didi

javascript - 如何在 Javascript 中存储特定类的对象数组?

转载 作者:行者123 更新时间:2023-12-01 01:58:34 25 4
gpt4 key购买 nike

假设我有一个这样定义的 Javascript 类:

class TmpTestResult {
constructor(id, title, testresult, tags, subsystem, info){
this.ID = id;
this.Title = title;
this.TestResult = testresult;
this.Tags = tags;
this.Subystem = subsystem;
this.Info = info;
}

如何在 Javascript 中存储 TmpTestResults 数组?

(我已经习惯了强类型 C#,所以我不知道如何做到这一点。)

我的示例数据如下所示:

function GetFakeData() {
var fakeTestResultArray = {
"jsonData": "",
"listResults": [{
"MyViewName": "Test View",
"ID": "10233",
"Title": "Verify the Production data is working.",
"TestResult": "Pass",
"Tags": "ATAB, Production, MOCK",
"Subsystem": "TEST",
"Info": "OK : OK"
},
{
"MyViewName": "Test View",
"ID": "54875",
"Title": "Verify the Production data is working one more time.",
"TestResult": "Pass",
"Tags": "ATAB, Production, MOCK",
"Subsystem": "TEST",
"Info": "OK : OK"
},
{
"MyViewName": "Test View",
"ID": "87541",
"Title": "Verify the Production data is working for a third.",
"TestResult": "Pass",
"Tags": "ATAB, Production, MOCK",
"Subsystem": "TEST",
"Info": "OK : OK"
}],
"MyViewName": "TEST Tests",
"ErrorInfo": "none",
"Count" : 0
}

最佳答案

您可以使用.map并返回TmpTestResult对象,例如

fakeTestResultArray.listResults.map(o => new TmpTestResult(o.ID, o.Title, o.TestResult, o.Tags, o.Subsystem, o.Info));

这是片段

class TmpTestResult {
constructor(id, title, testresult, tags, subsystem, info) {
this.ID = id;
this.Title = title;
this.TestResult = testresult;
this.Tags = tags;
this.Subystem = subsystem;
this.Info = info;
}
}
var fakeTestResultArray = {
"jsonData": "",
"listResults": [{
"MyViewName": "Test View",
"ID": "10233",
"Title": "Verify the Production data is working.",
"TestResult": "Pass",
"Tags": "ATAB, Production, MOCK",
"Subsystem": "TEST",
"Info": "OK : OK"
},
{
"MyViewName": "Test View",
"ID": "54875",
"Title": "Verify the Production data is working one more time.",
"TestResult": "Pass",
"Tags": "ATAB, Production, MOCK",
"Subsystem": "TEST",
"Info": "OK : OK"
},
{
"MyViewName": "Test View",
"ID": "87541",
"Title": "Verify the Production data is working for a third.",
"TestResult": "Pass",
"Tags": "ATAB, Production, MOCK",
"Subsystem": "TEST",
"Info": "OK : OK"
}
],
"MyViewName": "TEST Tests",
"ErrorInfo": "none",
"Count": 0
}



var res = fakeTestResultArray.listResults.map(o => new TmpTestResult(o.ID, o.Title, o.TestResult, o.Tags, o.Subsystem, o.Info));

console.log(res)

关于javascript - 如何在 Javascript 中存储特定类的对象数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50802131/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com