以前,我数组的结构是:
[
{
"id": "Ddfwe23224533",
"kind": "PEAK_EXPERIENCE",
"title": "IP - Collective Identity",
},
{
"id": "Ddfwe23224534",
"kind": "PEAK_EXPERIENCE",
"title": "IP - Collective Identity 2",
},
.....
]
我创建了一个名为flatToHierarchy的函数来对数组中的元素进行分组。这是代码:
export const flatToHierarchy = (arr, prop) => arr.reduce((accumulator, currentItem) => {
const items = accumulator;
const propValue = currentItem[prop];
items[propValue] = items[propValue]
? [...items[propValue], currentItem]
: [currentItem];
return items;
}, {});
现在,如果我这样称呼它:
flatToHierarchy(elements, 'kind')
它工作正常。
现在,数组的结构已更改。现在看起来像:
[
{
"entity": {
"id": "Ddfwe23224533",
"kind": "PEAK_EXPERIENCE",
"title": "IP - Collective Identity",
},
entity_id: "jj98834jfj983"
},
{
"entity": {
"id": "Ddfwe23224534",
"kind": "PEAK_EXPERIENCE",
"title": "IP - Collective Identity 2",
},
entity_id: "jj98834jfdf83"
},
.....
]
因此,现在按元素种类对数组进行分组,我这样称呼:
flatToHierarchy(elements, 'entity.kind')
现在,它给我的东西像:
{
undefined: [....element1, ....element2 ]
}
但是我想要的是:
{
PEAK_EXPERIENCE: [....element1, ....element2 ]
}
我对此进行了更深入的研究,发现无法以这种方式访问数组:
array['something.something']
需要通过以下方式进行访问:
array['something']['something']
因此,我更改了功能,但被卡住了。这是我更改的功能:
export const flatToHierarchy = (arr, prop) => arr.reduce((accumulator, currentItem) => {
const items = accumulator;
const props = prop.split('.').map(p => [p]);
console.log(props);
const propValue = currentItem[prop];
console.log(currentItem...props); // how to spread the array here?
items[propValue] = items[propValue]
? [...items[propValue], currentItem]
: [currentItem];
return items;
}, {});
更新:
当前输入的示例:
[
{
"entity": {
"id": "Ddfwe23224533",
"kind": "PEAK_EXPERIENCE",
"title": "IP - Collective Identity",
},
entity_id: "jj98834jfj983"
},
{
"entity": {
"id": "Ddfwe23224534",
"kind": "PEAK_EXPERIENCE",
"title": "IP - Collective Identity 2",
},
entity_id: "jj98834jfdf83"
},
{
"entity": {
"id": "Ddfwe23224594",
"kind": "PEAK_EXPERIENCE2",
"title": "IP - Collective Identity 6",
},
entity_id: "jj98834jfdf33"
},
]
所需输出的样本:
{
PEAK_EXPERIENCE: [
{
"entity": {
"id": "Ddfwe23224533",
"kind": "PEAK_EXPERIENCE",
"title": "IP - Collective Identity",
},
entity_id: "jj98834jfj983"
},
{
"entity": {
"id": "Ddfwe23224534",
"kind": "PEAK_EXPERIENCE",
"title": "IP - Collective Identity 2",
},
entity_id: "jj98834jfdf83"
},
],
PEAK_EXPERIENCE2: [
{
"entity": {
"id": "Ddfwe23224594",
"kind": "PEAK_EXPERIENCE2",
"title": "IP - Collective Identity 6",
},
entity_id: "jj98834jfdf33"
},
],
}
更新2:
在更改所需的功能时,我需要记住,该功能将始终具有以下指定的签名:
flatToHierarchy(array, string);
示例:
flatToHierarchy(elements, 'kind');
另一个例子:
flatToHierarchy(elements, 'entity.kind');
答案 0 :(得分:4)
您正在寻找一个path
函数,可以通过使用reduce
并向初始对象添加种子来实现。
当心null
/ undefined
属性!
const propValue = prop
.split('.') // Make a list of all property names (cannot contain a .)
.reduce((obj, p) => obj[p], currentItem); // Loop over them to traverse your object
const flatToHierarchy = (arr, prop) => arr.reduce((accumulator, currentItem) => {
const items = accumulator;
const propValue = prop.split('.').reduce((obj, p) => obj[p], currentItem);
items[propValue] = items[propValue]
? [...items[propValue], currentItem]
: [currentItem];
return items;
}, {});
const data = [
{
"entity": {
"id": "Ddfwe23224533",
"kind": "PEAK_EXPERIENCE",
"title": "IP - Collective Identity",
},
entity_id: "jj98834jfj983"
},
{
"entity": {
"id": "Ddfwe23224534",
"kind": "PEAK_EXPERIENCE",
"title": "IP - Collective Identity 2",
},
entity_id: "jj98834jfdf83"
},
{
"entity": {
"id": "Ddfwe23224594",
"kind": "PEAK_EXPERIENCE2",
"title": "IP - Collective Identity 6",
},
entity_id: "jj98834jfdf33"
},
]
console.log(
flatToHierarchy(data, "entity.kind")
);
答案 1 :(得分:0)
考虑到样本输入位于名为 data 的变量中,您可以执行以下操作。
data = data.reduce((output, current) => {
const kind = current.entity.kind
output[kind] = (output[kind] || []).concat(current)
return output
}, {})