使用类型脚本将Json转换为数组Angular2 js

时间:2017-01-12 18:07:03

标签: javascript json angular typescript

我想要将json数据转换为具有指定键的数组。

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

代码:

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

我只需要url键值。

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

`

2 个答案:

答案 0 :(得分:1)

应该是:

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

答案 1 :(得分:0)

如果使用lodash那么..

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

或ES6和lodash ..

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

或ES6,没有lodash ..

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

或ES5,没有lodash ..

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