0

I am trying to make an angular HttpClient get request but am having trouble with doing anything with the data.

I am using an api which is returning data in the form

(10)[{...}, {...}, {...}, ...]
[
0:{
   row: Array(4)
     0: "someid"
     1: "somename"
     2: "someaddress"
     3: "somepostcode"
  },
1:{
   row: Array(4)
     0: "someid"
     1: "somename"
     2: "someaddress"
     3: "somepostcode"
  },
2:{
   row: Array(4)
     0: "someid"
     1: "somename"
     2: "someaddress"
     3: "somepostcode"
  }, 
...
]

I just want to be able to create a list of objects with "someaddress" and "someid" fields from the get data. I have looked at various tutorials but keep getting undefined elements. How could you get the data from this.

2
  • you are getting an array of objects . run a loop and create your object Commented Mar 16, 2018 at 10:02
  • share your code Commented Mar 16, 2018 at 10:05

2 Answers 2

4

You can do like this:

const data = [ // I have reformatted your returned data
    { row: ["someId", "somename", "someaddress"] },
    { row: ["someId", "somename", "someaddress"] },
    { row: ["someId", "somename", "someaddress"] },
    { row: ["someId", "somename", "someaddress"] }
];
    
const newData = data.map(item => ({ id: item.row[0], address: item.row[2] }));
    
console.log(newData);

Hope it helps! :)

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

Comments

0

If you want to type your HTTP responses, use the generic type :

interface MyInterface {
  row: string[];
}

return this.http.get<MyInterface[]>('URL');

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.