2017-03-18 80 views
1

我想通过json数据创建新的数组但我不知道创建对象! 作秀页(环路)如何设置数组打印对象

的json数据

"LIBRARIES":{ 
"LIBRARY":[ 
{ 
"status":"available", 
"callNumber":"123456" 
}, 
{ 
"status":"available", 
"callNumber":"434356" 
} 
] 
} 

"search":{ 
"lsr02":[ 
"31011103618567", 
"31011001644160" 
]} 

我要创建的对象来存储这些数据

我想

"NEWDATA":{ 
"NEW":[ 
{ 
"status":"available", 
"callNumber":"123456", 
"lsr02": "31011103618567" ///set lsr02 to store in NEW 
}, 
{ 
"status":"available", 
"callNumber":"434356" 
"lsr02":"31011001644160" 
} 
] 
} 

我尝试

let details: string[] = []; 
for (let x of this.item.LIBRARIES.LIBRARY){ 
     details.push(x); 
    } 
    for (let x of this.item.search.lsr02){ 
     details.push(x); 
    } 
    console.log(details) 

的console.log(详细信息)显示

{ 
"status":"available", 
"callNumber":"123456" 
}, 
{ 
"status":"available", 
"callNumber":"434356" 
} 
{ 
"31011103618567" 
}, 
{ 
"31011001644160" 
} 

感谢您的帮助:)

回答

2

您正在推动搜索对象分开。您需要将它们分配给适当的库对象。尝试这个;

this.item = { 
 
    "LIBRARIES": { 
 
    "LIBRARY": [{ 
 
     "status": "available", 
 
     "callNumber": "123456" 
 
     }, 
 
     { 
 
     "status": "available", 
 
     "callNumber": "434356" 
 
     } 
 
    ] 
 
    }, 
 
    "search": { 
 
    "lsr02": [ 
 
     "31011103618567", 
 
     "31011001644160" 
 
    ] 
 
    } 
 
} 
 

 

 

 

 
let details = []; 
 

 

 
for (let i = 0; i < this.item.LIBRARIES.LIBRARY.length; i++) { 
 
    let lib = this.item.LIBRARIES.LIBRARY[i]; 
 
    lib.lsr02 = this.item.search.lsr02[i] 
 
    details.push(lib); 
 
} 
 

 
console.log(details)

1
export class NEWDATA { 
public status:string; 
public callNumber:string; 
public lsr02 : string; 

    constractor(_status : string, _callNumber : string, _lsr02 : string){ 
    this.status = _status; 
    this.callNumber = _callNumber; 
    this.lsr02 = _lsr02; 
    } 
} 

details : Array<NEWDATA> = []; 

for (let i = 0; i < this.item.LIBRARIES.LIBRARY.length; i++) { 
    details.push(new NEWDATA(this.item.LIBRARIES.LIBRARY[i].status, this.item.LIBRARIES.LIBRARY[i].callNumber, this.item.search.lsr02[i])); 
}