【发布时间】:2019-08-10 16:11:54
【问题描述】:
我想过滤以下 JSON 结果。但我无法转换。
const result =
[
{
id: 'e7a51e2a-384c-41ea-960c-bcd00c797629',
type: 'Interstitial (320x480)',
country: 'ABC',
enabled: true,
rank: 1,
link: '',
}];
到
[{
"Interstitial (320x480)" : {
"enabled": true,
"rank": 1,
}];
这是我的代码
const result =
[
{
id: 'e7a51e2a-384c-41ea-960c-bcd00c797629',
type: 'Interstitial (320x480)',
country: 'ABC',
enabled: true,
rank: 1,
link: '',
}];
const fields = ['type', 'rank', 'enabled'];
const tranform = Object.assign({}, ...fields
.filter(key => !!result[key]).map(key => ({ [key]: result[key] })));
console.log(tranform);
上面的代码,我想提取字段数组中提到的键
期望的结果应该是
[{
"Interstitial (320x480)" : {
"enabled": true,
"rank": 1,
}];
提前致谢。
【问题讨论】:
-
const fields = ['type', 'rank', 'enabled'];->第一个索引是对对象内的下一个索引进行分组的关键吗?
标签: javascript arrays node.js reactjs typescript