1

I want to create new array by json data But I have no idea to create Object! for show to page(loop for)

my json data

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

and

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

I want to create object to store this data

I want

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

and I try

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(details) show

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

thanks for your help :)

2 Answers 2

2

You are pushing the search objects separately. You need to assign them to appropriate library object. Try this;

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)

Sign up to request clarification or add additional context in comments.

Comments

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]));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.