如何在数组打字稿中设置Object

时间:2017-03-18 07:23:28

标签: javascript json angular typescript ionic2

我想通过json数据创建新数组但是我不知道创建Object! for show to page(循环)

我的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 个答案:

答案 0 :(得分: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)
&#13;
&#13;
&#13;

答案 1 :(得分: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]));
}