const parsers = new Map();
parsers._get = parsers.get;
parsers.get = function (key) {
if (this.has(key)) return this._get(key);
return () =>
console.warn(`Parser not implemented for: ${JSON.stringify(key)}`);
};
parsers.set("document", parseDocument);
parsers.set("paragraph", parseParagraph);
parsers.set("text", parseText);
parsers.set("hr", parseHR);
parsers.set("heading-4", parseH4);
parsers.set("heading-6", parseH6);
parsers.set("block", parseBlock);
function convert(obj) {
return [parsers.get(obj.nodeType)(obj)];
}
function parseDocument(obj) {
let type = "doc";
let attrs = { dirty: true };
let children = [];
obj.content.forEach((e) => children.push(parsers.get(e.nodeType)(e)));
return { type, children, attrs };
}
function parseParagraph(obj) {
let type = "p";
let attrs = { dirty: true };
let children = [];
obj.content.forEach((e) => children.push(parsers.get(e.nodeType)(e)));
return { type, children, attrs };
}
function parseText(obj) {
const result = {};
result.text = obj.value;
obj.marks.forEach((e) => (result[e.type] = true));
return result;
}
function parseHR(obj) {
let type = "hr";
let attrs = { dirty: true };
let children = [];
return { type, attrs, children };
}
function parseH4(obj) {
let type = "h4";
let attrs = { dirty: true };
let children = [];
obj.content.forEach((e) => children.push(parsers.get(e.nodeType)(e)));
children = children.map((c) => c.children).flat();
return { type, attrs, children };
}
function parseH6(obj) {
let type = "h6";
let attrs = { dirty: true };
let children = [];
obj.content.forEach((e) => children.push(parsers.get(e.nodeType)(e)));
children = children.map((c) => c.children).flat();
return { type, attrs, children };
}
function parseBlock(obj) {
let jsonObject = {
MVJlVePWr6: {
e_id: "MVJlVePWr6",
name_placeholder: "news_item",
alternative_tags: ["da", "es"],
},
gevhdjsx: {
e_id: "gevhdjsx",
name_placeholder: "changes_item",
alternative_tags: ["da"],
},
};
let type, attrs, children;
for (
i = 0;
jsonObject[obj.data.target.sys.id].alternative_tags.length > i;
i++
) {
for (const alt of jsonObject[obj.data.target.sys.id].alternative_tags) {
type = "block";
attrs = { dirty: true };
children = {};
if (obj.data.target.sys.id in jsonObject) {
children = {
type: "primary",
name: `${alt}_name_we_got`,
other_name: alt,
employees: "intern",
};
}
return { type, attrs, children };
}
}
}
let result = convert(getSrcData());
console.log("Converted object: ", result);
function getSrcData() {
return {
nodeType: "document",
data: {},
content: [
{
nodeType: "paragraph",
content: [
{
nodeType: "text",
value: "dummy testing bold",
marks: [
{
type: "bold",
},
],
data: {},
},
],
data: {},
},
{
nodeType: "hr",
content: [],
data: {},
},
{
nodeType: "paragraph",
content: [
{
nodeType: "text",
value: "",
marks: [],
data: {},
},
],
data: {},
},
{
nodeType: "heading-4",
content: [
{
nodeType: "text",
value: "heading 4",
marks: [
{
type: "bold",
},
],
data: {},
},
],
data: {},
},
{
nodeType: "heading-6",
content: [
{
nodeType: "text",
value: "heading 6",
marks: [
{
type: "italic",
},
],
data: {},
},
],
data: {},
},
{
nodeType: "hr",
content: [],
data: {},
},
{
nodeType: "block",
content: [],
data: {
target: {
sys: {
id: "MVJlVePWr6",
type: "Link",
linkType: "Block",
},
},
},
},
],
};
}
我已经编写了这段代码,我得到了预期的输出,问题是我想从一个函数(来自parseBlock )发送多个数据,但是我只能返回一个值,第一个是da,但我不能返回其他en值。
当我检查和打印console.log()时,我得到了多个值,但是当我返回值时,我得到的是单个值
由于我的预期输出类似于来自parseBlock的输出,但在上面的输出中,我从parseBlock获得单个值
{
"type": "block",
"attrs": {
"dirty": true
},
"children": {
"type": "primary",
"name": "da_name_we_got",
"other_name": "da",
"employees": "intern"
}
},
{
"type": "block",
"attrs": {
"dirty": true
},
"children": {
"type": "primary",
"name": "es_name_we_got",
"other_name": "es",
"employees": "intern"
}
}但这不是我要得到的,我也不知道我在哪里做错了从一个函数返回多个值
发布于 2022-04-28 07:42:27
问题
您的parseBlock函数有两个问题。
1.早日返回
下面的行中断了循环,这就是为什么循环只运行一次,而不是更多。
return { type, attrs, children };引入了一个结果数组。
const result = [];处理后的数据将被推送到结果中,
结果将是for循环之后的return。
return result;2.循环冗余
下面的部分将导致类型为block的4个对象。由于没有使用i,所以删除了这些行。
for (
i = 0;
jsonObject[obj.data.target.sys.id].alternative_tags.length > i;
i++
) {
...
}实例结果
const parsers = new Map();
parsers._get = parsers.get;
parsers.get = function (key) {
if (this.has(key)) return this._get(key);
return () =>
console.warn(`Parser not implemented for: ${JSON.stringify(key)}`);
};
parsers.set("document", parseDocument);
parsers.set("paragraph", parseParagraph);
parsers.set("text", parseText);
parsers.set("hr", parseHR);
parsers.set("heading-4", parseH4);
parsers.set("heading-6", parseH6);
parsers.set("block", parseBlock);
function convert(obj) {
return [parsers.get(obj.nodeType)(obj)];
}
function parseDocument(obj) {
let type = "doc";
let attrs = { dirty: true };
let children = [];
obj.content.forEach((e) => children.push(parsers.get(e.nodeType)(e)));
return { type, children, attrs };
}
function parseParagraph(obj) {
let type = "p";
let attrs = { dirty: true };
let children = [];
obj.content.forEach((e) => children.push(parsers.get(e.nodeType)(e)));
return { type, children, attrs };
}
function parseText(obj) {
const result = {};
result.text = obj.value;
obj.marks.forEach((e) => (result[e.type] = true));
return result;
}
function parseHR(obj) {
let type = "hr";
let attrs = { dirty: true };
let children = [];
return { type, attrs, children };
}
function parseH4(obj) {
let type = "h4";
let attrs = { dirty: true };
let children = [];
obj.content.forEach((e) => children.push(parsers.get(e.nodeType)(e)));
children = children.map((c) => c.children).flat();
return { type, attrs, children };
}
function parseH6(obj) {
let type = "h6";
let attrs = { dirty: true };
let children = [];
obj.content.forEach((e) => children.push(parsers.get(e.nodeType)(e)));
children = children.map((c) => c.children).flat();
return { type, attrs, children };
}
function parseBlock(obj) {
let jsonObject = {
MVJlVePWr6: {
e_id: "MVJlVePWr6",
name_placeholder: "news_item",
alternative_tags: ["da", "es"],
},
gevhdjsx: {
e_id: "gevhdjsx",
name_placeholder: "changes_item",
alternative_tags: ["da"],
},
}
let type, attrs, children;
const result = [];
// there was one addition for loop here, removed
for (const alt of jsonObject[obj.data.target.sys.id].alternative_tags) {
type = "block";
attrs = { dirty: true };
children = {};
if (obj.data.target.sys.id in jsonObject) {
children = {
type: "primary",
name: `${alt}_name_we_got`,
other_name: alt,
employees: "intern",
};
}
// return here was caused the loop to break
result.push({ type, attrs, children });
}
return result
}
let result = convert(getSrcData());
console.log("Converted object: ", result);
function getSrcData() {
return {
nodeType: "document",
data: {},
content: [
{
nodeType: "paragraph",
content: [
{
nodeType: "text",
value: "dummy testing bold",
marks: [
{
type: "bold",
},
],
data: {},
},
],
data: {},
},
{
nodeType: "hr",
content: [],
data: {},
},
{
nodeType: "paragraph",
content: [
{
nodeType: "text",
value: "",
marks: [],
data: {},
},
],
data: {},
},
{
nodeType: "heading-4",
content: [
{
nodeType: "text",
value: "heading 4",
marks: [
{
type: "bold",
},
],
data: {},
},
],
data: {},
},
{
nodeType: "heading-6",
content: [
{
nodeType: "text",
value: "heading 6",
marks: [
{
type: "italic",
},
],
data: {},
},
],
data: {},
},
{
nodeType: "hr",
content: [],
data: {},
},
{
nodeType: "block",
content: [],
data: {
target: {
sys: {
id: "MVJlVePWr6",
type: "Link",
linkType: "Block",
},
},
},
},
],
};
}
上面只有两个block。
https://stackoverflow.com/questions/72003660
复制相似问题