作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设我有一个这样定义的 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/
我是一名优秀的程序员,十分优秀!