如何转换 属性 值将用作键的对象结构?
How to transform an object structure where a property value is going to serve as key?
例如我有这个对象:
let obj = { id: "12", name: "abc" };
我需要将这个 obj 转换成这样:
{ "12": "abc" }; // the key is the value of “id”
这样,我就可以通过 id 访问 name,如下所示:
let name = obj["12"];
编辑:
我需要转换以下对象:
obj = {
"groups": [{
"groupId": 2345,
"status": 2
}, {
"groupId": 3456,
"status": 5
}
]
}
收件人:
obj ={
"2345":2,
"3456":5
}
这将使我能够使用 groupId
轻松查找状态。
例如:要获得状态5,我可以这样看obj["2345"]
因为 OP 可以直接 resolve/flatten groups
数组项进入目标 object/structure ... 来自 ...
{
groups: [{
groupId: 2345,
status: 2,
}, {
groupId: 3456,
status: 5,
}],
}
...进入...
{
"2345": 2,
"3456": 5
}
... 一个函数可能会基于 Array.prototype.reduce
and Object.assign
实现一种方法,对于每次迭代,它确实通过新的键值对聚合目标对象,并且相应的数据(groupId
, status
) 由迭代的 groups
数组的每个项目传递。最终返回的 target
对象是 reducer 函数的累加器,最初作为简单的空对象文字 {}
.
提供
上述方法有两种实现方式。第一个 ... resolveGroupsVerbose
... 初学者可能更容易 read/comprehend 因为代码明确说明了所做的事情,第二种方法 ... resolveGroups
... 使用Destructuring assignments 的结果是更紧凑的代码 ...
// One and the same approach ...
// ... without any destructuring and destructuring assignments.
function resolveGroupsVerbose(value) {
return value.groups.reduce((target, groupsItem) => {
// create new object ...
const obj = {};
// ... and assign new key-value data
// derived from `groupsItem`.
obj[groupsItem.groupId] = groupsItem.status;
// return the aggregated target.
return Object.assign(target, obj);
} , {});
}
// ... with destructuring assignments.
function resolveGroups({ groups }) {
return groups
.reduce((target, { groupId, status }) =>
Object.assign(target, { [groupId]: status }), {}
);
}
const sample = {
groups: [{
groupId: 2345,
status: 2,
}, {
groupId: 3456,
status: 5,
},
]};
console.log(
'resolveGroupsVerbose(sample) ...',
resolveGroupsVerbose(sample)
);
console.log(
'resolveGroups(sample) ...',
resolveGroups(sample)
);
// expected: resolveGroups(sample) ... {
// "2345": 2,
// "3456": 5
// }
.as-console-wrapper { min-height: 100%!important; top: 0; }
例如我有这个对象:
let obj = { id: "12", name: "abc" };
我需要将这个 obj 转换成这样:
{ "12": "abc" }; // the key is the value of “id”
这样,我就可以通过 id 访问 name,如下所示:
let name = obj["12"];
编辑:
我需要转换以下对象:
obj = {
"groups": [{
"groupId": 2345,
"status": 2
}, {
"groupId": 3456,
"status": 5
}
]
}
收件人:
obj ={
"2345":2,
"3456":5
}
这将使我能够使用 groupId
轻松查找状态。
例如:要获得状态5,我可以这样看obj["2345"]
因为 OP 可以直接 resolve/flatten groups
数组项进入目标 object/structure ... 来自 ...
{
groups: [{
groupId: 2345,
status: 2,
}, {
groupId: 3456,
status: 5,
}],
}
...进入...
{
"2345": 2,
"3456": 5
}
... 一个函数可能会基于 Array.prototype.reduce
and Object.assign
实现一种方法,对于每次迭代,它确实通过新的键值对聚合目标对象,并且相应的数据(groupId
, status
) 由迭代的 groups
数组的每个项目传递。最终返回的 target
对象是 reducer 函数的累加器,最初作为简单的空对象文字 {}
.
上述方法有两种实现方式。第一个 ... resolveGroupsVerbose
... 初学者可能更容易 read/comprehend 因为代码明确说明了所做的事情,第二种方法 ... resolveGroups
... 使用Destructuring assignments 的结果是更紧凑的代码 ...
// One and the same approach ...
// ... without any destructuring and destructuring assignments.
function resolveGroupsVerbose(value) {
return value.groups.reduce((target, groupsItem) => {
// create new object ...
const obj = {};
// ... and assign new key-value data
// derived from `groupsItem`.
obj[groupsItem.groupId] = groupsItem.status;
// return the aggregated target.
return Object.assign(target, obj);
} , {});
}
// ... with destructuring assignments.
function resolveGroups({ groups }) {
return groups
.reduce((target, { groupId, status }) =>
Object.assign(target, { [groupId]: status }), {}
);
}
const sample = {
groups: [{
groupId: 2345,
status: 2,
}, {
groupId: 3456,
status: 5,
},
]};
console.log(
'resolveGroupsVerbose(sample) ...',
resolveGroupsVerbose(sample)
);
console.log(
'resolveGroups(sample) ...',
resolveGroups(sample)
);
// expected: resolveGroups(sample) ... {
// "2345": 2,
// "3456": 5
// }
.as-console-wrapper { min-height: 100%!important; top: 0; }