underscore.js:_.pick 提取数组中对象内的属性
underscore.js: _.pick to extract properties inside an object that's in an array
下面是我拥有的对象:
{
"email": "joe@example.com",
"id": null,
"firstName": null,
"lastName": null,
"createdAt": "2016-10-05T18:16:07.000Z",
"updatedAt": "2016-10-05T18:16:07.000Z",
"Details": [
{
"id": 1,
"token": null,
"deviceId": null,
"code": 12345678,
"verified": null,
"createdAt": "2016-10-05T18:16:07.000Z",
"updatedAt": "2016-10-05T18:16:07.000Z",
"UserEmail": "joe@example.com"
}
]
}
我希望使用 pick method from the underscore.js 库和 return 以下对象:
{
"email": "joe@example.com",
"firstName": null,
"lastName": null,
"Details": [
{
"token": null,
"deviceId": null,
"code": 12345678,
"verified": null,
}
]
}
我试过使用:
var modifiedObj = _.pick(obj, 'email', 'firstName', 'lastName');
哪个 returns:
{
"email": "joe@example.com",
"firstName": null,
"lastName": null
}
如何提取 Details
对象以及其中的部分 properties
?
也许这行得通
var modifiedObj = _.pick(obj, 'email', 'firstName', 'lastName','Details[0].token','Details[0].deviceId','Details[0].code','Details[0].verified');
与其在多个级别上进行选择,我宁愿这样做
var obj = {
"email": "joe@example.com",
"id": null,
"firstName": null,
"lastName": null,
"createdAt": "2016-10-05T18:16:07.000Z",
"updatedAt": "2016-10-05T18:16:07.000Z",
"Details": [
{
"id": 1,
"token": null,
"deviceId": null,
"code": 12345678,
"verified": null,
"createdAt": "2016-10-05T18:16:07.000Z",
"updatedAt": "2016-10-05T18:16:07.000Z",
"UserEmail": "joe@example.com"
}
]
};
var newObj = (({email, firstName, lastName, Details}) =>
({
email,
firstName,
lastName,
Details: Details.map(
({token, deviceId, code, verified}) => ({token, deviceId, code, verified}))
})
)(obj);
console.log(newObj);
在这个模式中,我们"pick"通过编写一个函数来使用 ES6 参数解构,我们将一个对象传递给它,并从参数列表中的那个对象中提取我们想要的属性,({p1, p2}) =>
语法。然后,对于函数的 return 值,我们使用对象字面量 shorthand 指定一个仅包含这些属性的新对象,这使我们只需编写 {p1, p2}
即可获得 [=16] 的等价物=].所以最简单的情况,选择属性 p1
和 p1
,将是
(({p1, p2}) => ({p2, p2}))(obj)
在上面的例子中,我们在顶层使用了一次这个模式,然后再次从 Details
数组的每个元素中选择,通过 map
.
当然,如果你真的想,或者觉得上面的内容太混乱,你总是可以选择 "manually":
var newObj = {
email: obj.email,
...,
Details: obj.Details.map(function(detail) {
return {token: detail.token, ...};
})
};
使用_.pick
如果你还想使用_.pick
,那么你需要做两次:
var newObj = _.pick(obj, 'email', 'firstName', 'lastName', 'Details');
obj.Details = _.map(obj.Details, function(detail) {
return detail.pick('token', ...);
});
下面是我拥有的对象:
{
"email": "joe@example.com",
"id": null,
"firstName": null,
"lastName": null,
"createdAt": "2016-10-05T18:16:07.000Z",
"updatedAt": "2016-10-05T18:16:07.000Z",
"Details": [
{
"id": 1,
"token": null,
"deviceId": null,
"code": 12345678,
"verified": null,
"createdAt": "2016-10-05T18:16:07.000Z",
"updatedAt": "2016-10-05T18:16:07.000Z",
"UserEmail": "joe@example.com"
}
]
}
我希望使用 pick method from the underscore.js 库和 return 以下对象:
{
"email": "joe@example.com",
"firstName": null,
"lastName": null,
"Details": [
{
"token": null,
"deviceId": null,
"code": 12345678,
"verified": null,
}
]
}
我试过使用:
var modifiedObj = _.pick(obj, 'email', 'firstName', 'lastName');
哪个 returns:
{
"email": "joe@example.com",
"firstName": null,
"lastName": null
}
如何提取 Details
对象以及其中的部分 properties
?
也许这行得通
var modifiedObj = _.pick(obj, 'email', 'firstName', 'lastName','Details[0].token','Details[0].deviceId','Details[0].code','Details[0].verified');
与其在多个级别上进行选择,我宁愿这样做
var obj = {
"email": "joe@example.com",
"id": null,
"firstName": null,
"lastName": null,
"createdAt": "2016-10-05T18:16:07.000Z",
"updatedAt": "2016-10-05T18:16:07.000Z",
"Details": [
{
"id": 1,
"token": null,
"deviceId": null,
"code": 12345678,
"verified": null,
"createdAt": "2016-10-05T18:16:07.000Z",
"updatedAt": "2016-10-05T18:16:07.000Z",
"UserEmail": "joe@example.com"
}
]
};
var newObj = (({email, firstName, lastName, Details}) =>
({
email,
firstName,
lastName,
Details: Details.map(
({token, deviceId, code, verified}) => ({token, deviceId, code, verified}))
})
)(obj);
console.log(newObj);
在这个模式中,我们"pick"通过编写一个函数来使用 ES6 参数解构,我们将一个对象传递给它,并从参数列表中的那个对象中提取我们想要的属性,({p1, p2}) =>
语法。然后,对于函数的 return 值,我们使用对象字面量 shorthand 指定一个仅包含这些属性的新对象,这使我们只需编写 {p1, p2}
即可获得 [=16] 的等价物=].所以最简单的情况,选择属性 p1
和 p1
,将是
(({p1, p2}) => ({p2, p2}))(obj)
在上面的例子中,我们在顶层使用了一次这个模式,然后再次从 Details
数组的每个元素中选择,通过 map
.
当然,如果你真的想,或者觉得上面的内容太混乱,你总是可以选择 "manually":
var newObj = {
email: obj.email,
...,
Details: obj.Details.map(function(detail) {
return {token: detail.token, ...};
})
};
使用_.pick
如果你还想使用_.pick
,那么你需要做两次:
var newObj = _.pick(obj, 'email', 'firstName', 'lastName', 'Details');
obj.Details = _.map(obj.Details, function(detail) {
return detail.pick('token', ...);
});