1

I have this http service which returns an object, like for example this:

{ 
entries: [..,..,..,..],
entries2: [..,..,..],
somevalue: "the value",
somemore: "another value"
}

I'm using Angular's HttpClient, with a http.get(url).

i am able to do this:

http.get(url).map(result=>result['entries'])

when i subscribe on that, i only get the "entries". But i would like to have both the "entries", and "entries2" returned when i subscribe. I just don't understand how to do that. I've googled, for a few hours now, but I'm not even sure what to google for. ;)

So if anyone see what I'm trying to do, and would like to at least put me on the right track, please let me know :)

Thanks in advance.

1
  • You can try .map(result => ({entries: result.entries, entries2: result.entries2}));. Bear in mind there is no need to use the square bracket syntax here, so I've just replaced it with dot notation Commented Sep 29, 2018 at 10:02

1 Answer 1

0

Just use

.map (result => ({ 
  entries: result.entries,
  entries2: result.entries2 
}))

map can return anything actually. You can "project" result to any object you want.

If you want to return one big array which is the concatenation of entries and entries2, you could do something like this, for instance :

.map(result => result.entries.concat(result.entries2));

)

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

2 Comments

You may want ({ insead of just {. Braces after the => indicate function body, not object body.
Ah right, thank you ! I was just trying to run my first example, it wasn't working and I was temporarily going for the "return {}" version. Your suggestion is the way to go. edited.

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.