我的代码运行得很好,直到der在sections
中只有一个areas
为止。
现在我的数据结构发生了变化,在areas
内部,可以有多个sections
项目。
我以前使用下面的方式访问部分
const sections = data[0].areas[0].sections;
由于结构更改,我尝试了以下方法,
function parseAreas() {
data[0].areas.forEach(area => {
this.moveToNewObject(area);
})
}
工作示例https://jsfiddle.net/pkLyd0gh/
下面是我尝试过的方法,它会引发未定义的错误map
。
const data = {
data: [{
areas: [{
sections: [{
rjf: [{
"type": "heading-1",
"text": "A title",
},
{
"type": "ordered-list-item",
"text": "Ordered Item A",
},
{
"type": "unordered-list-item",
"text": "Ordered Item B",
},
{
"type": "heading-1",
"text": "A title",
}
]
}]
},
{
sections: [{
rjf: [{
"type": "heading-1",
"text": "Block 2 A title",
},
{
"type": "ordered-list-item",
"text": "Block 2 Ordered Item A",
},
{
"type": "unordered-list-item",
"text": "Block 2 Ordered Item B",
},
{
"type": "heading-1",
"text": "Block 2 A title",
}
]
}]
}
]
}]
};
function parseAreas() {
data[0].areas.forEach(area => {
this.moveToNewObject(area);
})
}
function moveToNewObject(data) {
console.log(data);
const sections = data.sections;
const listItemTypes = ['unordered-list-item', 'ordered-list-item'];
return sections.map((section) => {
let lastHeadingIndex = -1;
return section.rjf.reduce((acc, current, index) => {
if (!current.type || !listItemTypes.includes(current.type)) {
lastHeadingIndex = acc.length;
return [...acc, current]
} else {
let listObject = acc.find((el, i) => i > lastHeadingIndex && i < index && el.type === 'list');
if (!listObject) {
listObject = {
type: 'list',
items: [current]
}
return [...acc, listObject];
}
listObject.items = [...listObject.items, current];
return acc;
}
}, [])
});
}
console.log('sections', moveToNewObject(data.data));
答案 0 :(得分:0)
您的代码中有很多错误。例如。
const sections = data.sections;
在您的数组中,您有data->areas->sections
。但是您直接访问了未定义的data.sections
。
我已经修改了您的代码。请检查是否可以解决您的问题
const data = {
data: [{
areas: [{
sections: [{
rjf: [{
"type": "heading-1",
"text": "A title",
},
{
"type": "ordered-list-item",
"text": "Ordered Item A",
},
{
"type": "unordered-list-item",
"text": "Ordered Item B",
},
{
"type": "heading-1",
"text": "A title",
}
]
}]
},
{
sections: [{
rjf: [{
"type": "heading-1",
"text": "Block 2 A title",
},
{
"type": "ordered-list-item",
"text": "Block 2 Ordered Item A",
},
{
"type": "unordered-list-item",
"text": "Block 2 Ordered Item B",
},
{
"type": "heading-1",
"text": "Block 2 A title",
}
]
}]
}
]
}]
};
function parseAreas() {
data[0].areas.forEach(area => {
this.moveToNewObject(area);
})
}
function moveToNewObject(data) {
const areas = data[0].areas;
//console.log(data[0].areas)
const listItemTypes = ['unordered-list-item', 'ordered-list-item'];
return areas.map((area) => {
var sec = area.sections;
return sec.map((section) => {
let lastHeadingIndex = -1;
return section.rjf.reduce((acc, current, index) => {
if (!current.type || !listItemTypes.includes(current.type)) {
lastHeadingIndex = acc.length;
return [...acc, current]
} else {
let listObject = acc.find((el, i) => i > lastHeadingIndex && i < index && el.type === 'list');
if (!listObject) {
listObject = {
type: 'list',
items: [current]
}
return [...acc, listObject];
}
listObject.items = [...listObject.items, current];
return acc;
}
}, [])
});
});
}
console.log('sections', moveToNewObject(data.data));
答案 1 :(得分:0)
您的解析函数需要使用data
作为参数,否则它将尝试访问全局定义的data
。让我们也将console.log语句移到该函数中,否则我们将看不到输出(或者,找出从parseAreas
返回所有输出的方法)。最后,您实际上需要调用该方法。
function parseAreas(data) {
data[0].areas.forEach(area => {
console.log('sections', JSON.stringify(moveToNewObject(area)));
})
}
parseAreas(data.data)