2

I have the json data i want to convert to an array with the specified keys.

[
  {"id":1,"url": "http://test.com"},
  {"id":2,"url":"https://ad.com/"},
  {"id":3,"url":"https://ad12.com/"},
  {"id":4,"url":"https://ad12.com/"}
]

Code:

 http.get('', { headers: ''})
     .map(var response => response.json();
     return response.map(d => {
     return new class(d.url);
    }););

I need only url key values.

Output:`["http://test.com", "http://ad.com", "http://ad12.com"]

`

4
  • well, why not hold it in model and apply map over it? Commented Jan 12, 2017 at 18:08
  • I have tried but not able to achieve it . Commented Jan 12, 2017 at 18:09
  • 2
    Show what you've tried please. Commented Jan 12, 2017 at 18:10
  • @echonax I have tried the above mentioned code. Commented Jan 12, 2017 at 18:21

2 Answers 2

1

It should be:

http.get('', { headers: ''})
    .map(response => {
        return response.json().map(d => {
            return new class(d.url);
        });
    });
Sign up to request clarification or add additional context in comments.

Comments

0

if using lodash then ..

var urls = _.map(yourArray, 'url');

or ES6 and lodash ..

let urls = _.map(yourArray, 'url');

or ES6 and no lodash ..

lets urls = yourArray.map((obj) => {
    return obj.url;
});

or ES5 and no lodash ..

lets urls = yourArray.map(function (obj) {
    return obj.url;
});

1 Comment

More idiomatic ES6 would be map(({url}) => url).

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.