2017-03-18 81 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])); 
}