我想通过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)
控制台.log(详情(展示
{
"status":"available",
"callNumber":"123456"
},
{
"status":"available",
"callNumber":"434356"
}
{
"31011103618567"
},
{
"31011001644160"
}
感谢您的帮助:)
您正在单独推送搜索对象。您需要将它们分配给适当的库对象。试试这个;
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)
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]));
}