我正在尝试从多维对象创建一维json对象。
我在Typescript中尝试过类似的事情。
export interface JsonInput {
ALIST: string[][];
BLIST: string[][];
CLIST: string[][];
DLIST: string[][];
}
export interface JsonOutput {
AID: string[];
BID: string[];
CID: string[];
DID: string[];
}
splitArray(objArray) {
if (objArray["ALIST"] != undefined && objArray["ALIST"].length > 0) {
for (var x = 0; x < objArray["ALIST"].length; x++) {
if (objArray["ALIST"][x].AID != undefined && objArray["ALIST"][x].AID != "")
this.JsonInput.ALIST = objArray["ALIST"][x].AID;
}
}
}
这是我作为输入参数收到的JSON。
{
"ALIST": [
{
"AID": "LWRNKSXR---01CAB101B",
"COMPANY": "A1"
},
{
"AID": "71/EGNC/912947/BGSO/911",
"COMPANY": "A2"
}
],
"BLIST": [
{
"BID": "E911",
"COMPANY": "B1"
},
{
"BID": "B11CBSIG576",
"COMPANY": "B2"
}
],
"CLIST": [
{
"CID": "7107230837",
"COMPANY": "C1"
},
{
"CID": "219S601761",
"COMPANY": "C2"
}
],
"DLIST": [
{
"DID": "UVERSE",
"COMPANY": "D1"
},
{
"DID": "COMPANY",
"COMPANY": "D2"
}
]
}
我正在尝试创建以下提到的输出。我想将第一个对象作为输入参数发送到微服务,第二个对象绑定公司dropdownlist。
{
"AID": ["LWRNKSXR---01CAB101B", "71/EGNC/912947/BGSO/911"],
"BID": ["E911", "B11CBSIG576"],
"CLIST": ["7107230837", "219S601761"],
"DLIST": ["UVERSE", "COMPANY"]
}
{
"COMPANY": ["A1","A2","B1","B2","C1","C2","D1","D2"]
}
答案 0 :(得分:0)
此函数将完成您要寻找的...我们遍历适当的对象以填充最终一维数组中相应对象的值
makeCustomObject() {
this.finalObj = {
"AID": [],
"BID": [],
"CLIST": [],
"DLIST": [],
"COMPANY": []
};
for (var i = 0; i < this.jsonObject.ALIST.length; i++) {
this.finalObj.AID.push(this.jsonObject.ALIST[i].AID);
this.finalObj.COMPANY.push(this.jsonObject.ALIST[i].COMPANY);
}
for (var i = 0; i < this.jsonObject.BLIST.length; i++) {
this.finalObj.BID.push(this.jsonObject.BLIST[i].BID);
this.finalObj.COMPANY.push(this.jsonObject.BLIST[i].COMPANY);
}
for (var i = 0; i < this.jsonObject.CLIST.length; i++) {
this.finalObj.CLIST.push(this.jsonObject.CLIST[i].CID);
this.finalObj.COMPANY.push(this.jsonObject.CLIST[i].COMPANY);
}
for (var i = 0; i < this.jsonObject.DLIST.length; i++) {
this.finalObj.DLIST.push(this.jsonObject.DLIST[i].DID);
this.finalObj.COMPANY.push(this.jsonObject.DLIST[i].COMPANY);
}
}
答案 1 :(得分:0)
如果无法使用flatMap,请尝试减少操作。
const data = {
"ALIST": [
{
"AID": "LWRNKSXR---01CAB101B",
"COMPANY": "A1"
},
{
"AID": "71/EGNC/912947/BGSO/911",
"COMPANY": "A2"
}
],
"BLIST": [
{
"BID": "E911",
"COMPANY": "B1"
},
{
"BID": "B11CBSIG576",
"COMPANY": "B2"
}
],
"CLIST": [
{
"CID": "7107230837",
"COMPANY": "C1"
},
{
"CID": "219S601761",
"COMPANY": "C2"
}
],
"DLIST": [
{
"DID": "UVERSE",
"COMPANY": "D1"
},
{
"DID": "COMPANY",
"COMPANY": "D2"
}
]
};
const process = data => Object.values(data).reduce((result, list) => list.reduce((result, item) => {
const key = Object.keys(item)[0];
if (result.data[key]) {
result.data[key].push(item[key]);
} else {
result.data[key] = [item[key]];
}
result.company.push(item.COMPANY);
return result;
}, result), { data: {}, company: [] });
console.log(process(data));