2

I am trying to figure out how to transpose response into a interface i created for storage.

i.e.

interface TestInterface {
name: string,
count: number
}

http.get("localhost:8088/api/getdata")
.map(res => res.json()
.subscribe(
(data) => {

// transpose the response into array of TestInterface

});

The response returns a array of objects in following format:

[
  {
    "Id": "54d130ce-0812-e711-b861-f01faf23929d",
    "Name": "First Name Attempt",
    "Description": "asd",
    "Count": 5
  },
  {
    "Id": "6f08ca4a-0d12-e711-b861-f01faf23929d",
    "Name": "Second Attempt",
    "Description": "One Site",
    "Count": 3
  }
]

1 Answer 1

2

Do it manually like this

interface TestInterface {
  name: string,
  count: number
}

http.get("localhost:8088/api/getdata")
  .map(res => res.json())
  .map((data: any[]) => {
    let arr: TestInterface[] = [];
    data.forEach(o => {
      let obj: TestInterface = {};
      obj.name = o.Name;
      obj.count = o.Count;
      arr.push(obj);
    });
    return arr;
  })
  .subscribe((data: TestInterface[]) => {
    // data here

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

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.