简体   繁体   中英

Converting JSON to Array Typescript

I have the following JSON

aegisLab: {scan_time: 2, def_time: "2020-07-09T07:57:00Z", scan_result_i: 0, threat_found: ""}
agnitum: null
ahnlab: {scan_time: 0, def_time: "2020-07-09T00:00:00Z", scan_result_i: 0, threat_found: ""}
avg: null
avira: {scan_time: 0, def_time: "2020-07-09T00:00:00Z", scan_result_i: 0, threat_found: ""}
baidu: null
bitDefender: {scan_time: 8, def_time: "2020-07-09T04:18:00Z", scan_result_i: 0, threat_found: ""}
byteHero: {scan_time: 204, def_time: "2020-07-07T00:00:00Z", scan_result_i: 0, threat_found: ""}
clamAV: {scan_time: 16, def_time: "2020-07-08T13:50:00Z", scan_result_i: 0, threat_found: ""}
cyren: {scan_time: 8, def_time: "2020-07-09T08:43:00Z", scan_result_i: 0, threat_found: ""}
drWebGateway: null
emsisoft: {scan_time: 16, def_time: "2020-07-08T23:38:00Z", scan_result_i: 0, threat_found: ""}
eset: {scan_time: 1, def_time: "2020-07-09T00:00:00Z", scan_result_i: 0, threat_found: ""}
filseclab: {scan_time: 173, def_time: "2020-06-22T00:09:00Z", scan_result_i: 0, threat_found: ""}
fortinet: {scan_time: 17, def_time: "2020-07-08T00:00:00Z", scan_result_i: 0, threat_found: ""}
fprot: null
fsecure: null
hauri: {scan_time: 4, def_time: "2020-07-09T00:00:00Z", scan_result_i: 0, threat_found: ""}
ikarus: {scan_time: 9, def_time: "2020-07-09T07:45:26Z", scan_result_i: 0, threat_found: ""}
jiangmin: {scan_time: 707, def_time: "2020-07-06T19:06:00Z", scan_result_i: 0, threat_found: ""}
k7: {scan_time: 0, def_time: "2020-07-09T07:29:00Z", scan_result_i: 0, threat_found: ""}
lavasoft: null
mcAfee: {scan_time: 1, def_time: "2020-07-08T00:00:00Z", scan_result_i: 0, threat_found: ""}
microsoftSecurityEssentials: null
nProtect: null
nanoav: {scan_time: 2, def_time: "2020-07-09T01:49:00Z", scan_result_i: 0, threat_found: ""}
preventon: {scan_time: 47, def_time: "2020-07-09T03:08:00Z", scan_result_i: 0, threat_found: ""}
quickHeal: null
sophos: {scan_time: 1, def_time: "2020-07-09T03:08:00Z", scan_result_i: 0, threat_found: ""}
stoPzilla: null
superAntiSpyware: {scan_time: 958, def_time: "2020-07-02T14:29:00Z", scan_result_i: 0, threat_found: ""}
threatTrack: null
totalDefense: null
trendMicro: {scan_time: 486, def_time: "2020-07-07T20:22:00Z", scan_result_i: 0, threat_found: ""}
trendMicroHouseCall: null
virITeXplorer: null
virusBlokAda: {scan_time: 14, def_time: "2020-07-09T08:37:00Z", scan_result_i: 0, threat_found: ""}
xvirusPersonalGuard: null
zillya: null
zoner: null
__proto__: Object

I'm trying to convert it into an array using the following code

this.value.scans = Object.keys(this.value).map(key => {
              this.completed = true;

                    return {
                        ...this.value[key],
                      scannerName: key,

                  };
            });

But this piece of code is returning inconsistent results.

Actually the shared is not a valid object. The comma is missing after every entry.

data ={aegisLab: {scan_time: 2, def_time: "2020-07-09T07:57:00Z", scan_result_i: 0, threat_found: ""},
agnitum: null,
ahnlab: {scan_time: 0, def_time: "2020-07-09T00:00:00Z", scan_result_i: 0, threat_found: ""},
avg: null,
avira: {scan_time: 0, def_time: "2020-07-09T00:00:00Z", scan_result_i: 0, threat_found: ""},
baidu: null,
bitDefender: {scan_time: 8, def_time: "2020-07-09T04:18:00Z", scan_result_i: 0, threat_found: ""},
byteHero: {scan_time: 204, def_time: "2020-07-07T00:00:00Z", scan_result_i: 0, threat_found: ""},
clamAV: {scan_time: 16, def_time: "2020-07-08T13:50:00Z", scan_result_i: 0, threat_found: ""},
cyren: {scan_time: 8, def_time: "2020-07-09T08:43:00Z", scan_result_i: 0, threat_found: ""},
drWebGateway: null,
emsisoft: {scan_time: 16, def_time: "2020-07-08T23:38:00Z", scan_result_i: 0, threat_found: ""},
eset: {scan_time: 1, def_time: "2020-07-09T00:00:00Z", scan_result_i: 0, threat_found: ""},
filseclab: {scan_time: 173, def_time: "2020-06-22T00:09:00Z", scan_result_i: 0, threat_found: ""},
fortinet: {scan_time: 17, def_time: "2020-07-08T00:00:00Z", scan_result_i: 0, threat_found: ""},
fprot: null,
fsecure: null,
hauri: {scan_time: 4, def_time: "2020-07-09T00:00:00Z", scan_result_i: 0, threat_found: ""},
ikarus: {scan_time: 9, def_time: "2020-07-09T07:45:26Z", scan_result_i: 0, threat_found: ""},
jiangmin: {scan_time: 707, def_time: "2020-07-06T19:06:00Z", scan_result_i: 0, threat_found: ""},
k7: {scan_time: 0, def_time: "2020-07-09T07:29:00Z", scan_result_i: 0, threat_found: ""},
lavasoft: null,
mcAfee: {scan_time: 1, def_time: "2020-07-08T00:00:00Z", scan_result_i: 0, threat_found: ""},
microsoftSecurityEssentials: null,
nProtect: null,
nanoav: {scan_time: 2, def_time: "2020-07-09T01:49:00Z", scan_result_i: 0, threat_found: ""},
preventon: {scan_time: 47, def_time: "2020-07-09T03:08:00Z", scan_result_i: 0, threat_found: ""},
quickHeal: null,
sophos: {scan_time: 1, def_time: "2020-07-09T03:08:00Z", scan_result_i: 0, threat_found: ""},
stoPzilla: null,
superAntiSpyware: {scan_time: 958, def_time: "2020-07-02T14:29:00Z", scan_result_i: 0, threat_found: ""},
threatTrack: null,
totalDefense: null,
trendMicro: {scan_time: 486, def_time: "2020-07-07T20:22:00Z", scan_result_i: 0, threat_found: ""},
trendMicroHouseCall: null,
virITeXplorer: null,
virusBlokAda: {scan_time: 14, def_time: "2020-07-09T08:37:00Z", scan_result_i: 0, threat_found: ""},
xvirusPersonalGuard: null,
zillya: null,
zoner: null};

public res = Object.keys(this.data).map(key => {
                    return {
                        ...this.data[key],
                      scannerName: key,

                  };
            });

So I have corrected your input first. Now it's working

CheckThis

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM