假设我有一个以这种方式定义的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
}
答案 0 :(得分:1)
您只需使用标准数组并调用构造函数:
function GetFakeData() {
var fakeTestResultArray = {
"jsonData": "",
"listResults": [
new TmpTestResult(
"10233",
"Verify the Production data is working.",
"Pass",
"ATAB, Production, MOCK",
"TEST",
"OK : OK"
),
new TmpTestResult(/*...*/),
new TmpTestResult(/*...*/),
new TmpTestResult(/*...*/),
],
"MyViewName": "TEST Tests",
"ErrorInfo": "none",
"Count" : 0
}
您可能会考虑让构造函数接受一个对象(可能是可选的),因此您可以使用文字表示法:
class TmpTestResult {
constructor(id, title, testresult, tags, subsystem, info){
if (typeof id === "object") {
Object.assign(this, id);
} else {
this.ID = id;
this.Title = title;
this.TestResult = testresult;
this.Tags = tags;
this.Subystem = subsystem;
this.Info = info;
}
}
然后
function GetFakeData() {
var fakeTestResultArray = {
"jsonData": "",
"listResults": [
new TmpTestResult({
"MyViewName": "Test View",
"ID": "10233",
"Title": "Verify the Production data is working.",
"TestResult": "Pass",
"Tags": "ATAB, Production, MOCK",
"Subsystem": "TEST",
"Info": "OK : OK"
}),
new TmpTestResult({
// ...
}),
// ...
JavaScript没有C#的构造期间的属性赋值(无论它叫什么),你可以做new TmpTestResult { ID = "..."
...但是可选择接受一个对象。
答案 1 :(得分:1)
您可以使用.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)
答案 2 :(得分:0)
在JavaScript中,一切都是基本类型,如字符串或数字,或数组[]或对象{}。 将数据存储在可以具有嵌套对象的对象数组中。 基本上,fakeTestResultArray已经做得很好。
现在你应该找一个可以保存JSON对象的数据库。像MongoDB这样的东西。