spbsmile spbsmile - 11 days ago 6
JSON Question

create reactjs class from json

my aim: create js es6 class from json below

json:

{
"Firms": {
"Blacklist": [
{
"Description": "",
"Firms": [
{
"FirmId": 64843222,
"FirmName": "insanis2",
"Comment": null
}
]
}
],
"Neutral": [],
"Partners": []
}
}


my attemp js code. you can see.

i have problems with array.

how add reactjs class array fields ?

export default class FirmLists {
Firms
}

export default class Firms {
Blacklist //array
Neutral //array
}

export default class Firm {
FirmId: string = null;
FirmName: string = null;
Comment: string = null;
}

export default class Blacklist {
Description: string = null;
Firms //array
}

export default class Neutral {
Description: string = null;
Firms //array
}


This simple model from json

Answer

If you are really working on ES2015, you can assign values in constructor.

class Firm {
  constructor() {
    this.Blacklist = [];
    this.Neutral = [];
    this.Partners = [];

    this.Blacklist.push(new BlacklistItem(...))
  }
}

But I recommend you to confirm what kind of language are you writing. There is no type declaration like : string in JavaScript, even in ECMAScript 2015+.

Such type declaration syntax exists in TypeScript. You can also extend JS syntax using FlowJS. ReactJS is framework and it is not related to syntax.

Comments