取到数组中的对象中的部分属性值

Freya0608
  • 636

原始数据

[{
                    "Postcode": "",
                    "WorkSince": null,
                    "FundVolume": 300000000,
                    "Intro": null,
                    "type": "基金从业人员资格证",
                    "url": "Upload/UserCenter/20150303/2015030315252534569105.jpg",
                    "id": 19332
                },
                {
                    "Postcode": "",
                    "WorkSince": null,
                    "FundVolume": 300000000,
                    "Intro": null,
                    "type": "基金从业人员资格证",
                    "url": "Upload/UserCenter/20150303/2015030315252534569105.jpg",
                    "id": 19347
    }]
    

需要的数据:

[{
                    
                    "type": "基金从业人员资格证",
                    "url": "Upload/UserCenter/20150303/2015030315252534569105.jpg",
                    "id": 19332
                },
                {
                 
                    "type": "基金从业人员资格证",
                    "url": "Upload/UserCenter/20150303/2015030315252534569105.jpg",
                    "id": 19347
    }]
回复
阅读 7.7k
3 个回答
阿升
  • 555
✓ 已被采纳
var data = [{type: 1, url: 1, id: 1, a: 1}];
data.map(({type, url, id}) => {
    return {type, url, id};
});
var data = [{type: 1, url: 1, id: 1, a: 1}];
data.map(({type, url, id}) => ({type, url, id}));

两种写法其实一样,第一个种可读性好点,扩展性也好

var data = [ ... ];
data.map(function(v) {
    var obj = {};
    
    obj.type = v.type;
    obj.url = v.url;
    obj.id = v.id;
    
    return obj;
});
宣传栏