如何从js / javascript节点的返回值中删除null或undefined



我写了一个代码,我想从这里删除未定义或null值,我返回

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("table", parseTable);
parsers.set("tablerow", parseRow);
parsers.set("htr", parseHTR);
parsers.set("tablehc", parseHC);
parsers.set("tbody", parseTBody);
parsers.set("btr", parseBTR);
parsers.set("tablec", parseTableC);
function convert(obj) {
  return [parsers.get(obj.nodeType)(obj)];
}
function parseDocument(obj) {
  let type = "doc";
  let children = [];
  obj.content.forEach((e) => children.push(parsers.get(e.nodeType)(e)));
  return { type, children: children.filter((e) => e !== null) };
}
function parseParagraph(obj) {
  let type = "p";
  let children = [];
  obj.content.forEach((e) => children.push(parsers.get(e.nodeType)(e)));
  return { type, children: children.filter((e) => e !== null) };
}
function parseText(obj) {
  const result = {};
  result.text = obj.value;
  obj.marks.forEach((e) => (result[e.type] = true));
  return result;
}
function parseTable(obj) {
  let type = "table";
  let children = [];
  children.push(parsers.get("tbody")(obj));
  obj.content.forEach((e) => {
    children.push(parsers.get(e.nodeType)(e));
  });
  return { type, children: children.filter((e) => e !== null) };
}
function parseRow(obj) {
  var type = [];
  let children = [];
  
  let check=[];
  obj.content.forEach((e) => {
  function add(item) {
    if(check.indexOf(item) === -1) {
      check.push(item);
    }
  }
    add(e.nodeType);
  });
  
  if (check.join() === "tablehc") {
      type.push("thead");
      children.push(parsers.get("htr")(obj));
    } else {
   // getting null and undefined from here
    }
  type = [...new Set(type)].join();
  
  if(children.length>0)
  return { type, children: children.filter((e) => e !== null) };
}
function parseHTR(obj) {
  let type = "tr";
  let children = [];
  obj.content.forEach((e) => {
    children.push(parsers.get(e.nodeType)(e));
  });
  return { type, children: children.filter((e) => e !== null) };
}
function parseHC(obj) {
  let type = "th";
  let children = [];
  obj.content.forEach((e) => {
    children.push(parsers.get(e.nodeType)(e));
  });
  return { type, children: children.filter((e) => e !== null) };
}
function parseTBody(obj) {
  let type = "tbody";
  let children = [];
  children.push(parsers.get("btr")(obj));
  return { type, children: children.filter((e) => e !== null) };
}
function parseBTR(obj) {
  let type = "tr";
  let children = [];
  obj.content.forEach((e) => {
  if (e.nodeType === "tablec")
    children.push(parsers.get(e.nodeType)(e));
  });
  
  if (children.length > 0) return { type, children: children.filter((e) => e !== null) };
}
function parseTableC(obj) {
  let type = "td";
  let children = [];
  obj.content.forEach((e) => children.push(parsers.get(e.nodeType)(e)));
  return { type, children: children.filter((e) => e !== null) };
}
let result = convert(getSrcData());
console.log("Converted object: ", result);
function getSrcData() {
  return {
    nodeType: "document",
    content: [
      {
        nodeType: "paragraph",
        content: [
          {
            nodeType: "text",
            value: "dummy testing bold",
            marks: [
              {
                type: "bold",
              },
            ],
          },
        ],
      },
      {
        nodeType: "table",
        content: [
          {
            nodeType: "tablerow",
            content: [
              {
                nodeType: "tablehc",
                content: [
                  {
                    nodeType: "paragraph",
                    content: [
                      {
                        nodeType: "text",
                        value: "hey",
                        marks: [],
                      },
                    ],
                  },
                ],
              },
              {
                nodeType: "tablehc",
                content: [
                  {
                    nodeType: "paragraph",
                    content: [
                      {
                        nodeType: "text",
                        value: "",
                        marks: [],
                      },
                    ],
                  },
                ],
              },
            ],
          },
          {
            nodeType: "tablerow",
            content: [
              {
                nodeType: "tablec",
                content: [
                  {
                    nodeType: "paragraph",
                    content: [
                      {
                        nodeType: "text",
                        value: "code text",
                        marks: [
                          {
                            type: "code",
                          },
                        ],
                      },
                    ],
                  },
                ],
              },
            ],
          },
          {
            nodeType: "tablerow",
            content: [
              {
                nodeType: "tablec",
                content: [
                  {
                    nodeType: "paragraph",
                    content: [
                      {
                        nodeType: "text",
                        value: "text",
                        marks: [
                          {
                            type: "bold",
                          },
                        ],
                      },
                    ],
                  },
                ],
              },
            ],
          },
        ],
      },
      {
        nodeType: "paragraph",
        content: [
          {
            nodeType: "text",
            value: "",
            marks: [],
          },
        ],
      },
      {
        nodeType: "table",
        content: [
          {
            nodeType: "tablerow",
            content: [
              {
                nodeType: "tablehc",
                content: [
                  {
                    nodeType: "paragraph",
                    content: [
                      {
                        nodeType: "text",
                        value: "hey",
                        marks: [],
                      },
                    ],
                  },
                ],
              },
              {
                nodeType: "tablehc",
                content: [
                  {
                    nodeType: "paragraph",
                    content: [
                      {
                        nodeType: "text",
                        value: "",
                        marks: [],
                      },
                    ],
                  },
                ],
              },
              {
                nodeType: "tablehc",
                content: [
                  {
                    nodeType: "paragraph",
                    content: [
                      {
                        nodeType: "text",
                        value: "",
                        marks: [],
                      },
                    ],
                  },
                ],
              },
            ],
          },
          {
            nodeType: "tablerow",
            content: [
              {
                nodeType: "tablec",
                content: [
                  {
                    nodeType: "paragraph",
                    content: [
                      {
                        nodeType: "text",
                        value: "code text",
                        marks: [
                          {
                            type: "code",
                          },
                        ],
                      },
                    ],
                  },
                ],
              },
              {
                nodeType: "tablec",
                content: [
                  {
                    nodeType: "paragraph",
                    content: [
                      {
                        nodeType: "text",
                        value: "",
                        marks: [],
                      },
                    ],
                  },
                ],
              },
            ],
          },
          {
            nodeType: "tablerow",
            content: [
              {
                nodeType: "tablec",
                content: [
                  {
                    nodeType: "paragraph",
                    content: [
                      {
                        nodeType: "text",
                        value: "bold text",
                        marks: [
                          {
                            type: "bold",
                          },
                        ],
                      },
                    ],
                  },
                ],
              },
              {
                nodeType: "tablec",
                content: [
                  {
                    nodeType: "paragraph",
                    content: [
                      {
                        nodeType: "text",
                        value: "",
                        marks: [],
                      },
                    ],
                  },
                ],
              },
            ],
          },
        ],
      },
    ],
  };
}

有没有可能在没有这个未定义的null值的情况下获得输入值

我在这里得到未定义,但在我的系统中,我得到的是空值而不是未定义如果我可以在实际输出

中返回空值和未定义,是否有任何可能?作为我的期望值是这样的

预期输出:-

Converted object: [
  {
    "type": "doc",
    "children": [
      {
        "type": "p",
        "children": [
          {
            "text": "dummy testing bold",
            "bold": true
          }
        ]
      },
      {
        "type": "table",
        "children": [
          {
            "type": "head",
            "children": [
              {
                "type": "tr",
                "children": [
                  {
                    "type": "th",
                    "children": [
                      {
                        "type": "p",
                        "children": [{ "text": "hey" }]
                      }
                    ]
                  }
                ]
              }
            ]
          },
          {
            "type": "body",
            "children": [
              {
                "type": "tr",
                "children": [
                  {
                    "type": "td",
                    "children": [
                      {
                        "type": "p",
                        "children": [{ "text": "code text", "code": true }]
                      }
                    ]
                  }
                ]
              },
              {
                "type": "tr",
                "children": [
                  {
                    "type": "td",
                    "children": [
                      {
                        "type": "p",
                        "children": [{ "text": "bold text", "bold": true }]
                      }
                    ]
                  }
                ]
              }
            ]
          }
        ]
      },
      {
        "type": "p",
        "children": [
          {
            "text": ""
          }
        ]
      },
// table should look like this
      {
        "type": "table",
        "children": [
          {
// head part contains only the head part and its supporting children
            "type": "head",
            "children": [
              {
                "type": "tr",
                "children": [
                  {
                    "type": "th",
                    "children": [
                      {
                        "type": "p",
                        "children": [{ "text": "hey" }]
                      }
                    ]
                  },
                  {
                    "type": "th",
                    "children": [
                      {
                        "type": "p",
                        "children": [{ "text": "" }]
                      }
                    ]
                  }
                ]
              }
            ]
          },
// body part contains only the body part and its supporting children
          {
            "type": "body",
            "children": [
              {
                "type": "tr",
                "children": [
                  {
                    "type": "td",
                    "children": [
                      {
                        "type": "p",
                        "children": [{ "text": "code text", "code": true }]
                      }
                    ]
                  },
                  {
                    "type": "td",
                    "children": [
                      {
                        "type": "p",
                        "children": [{ "text": "" }]
                      }
                    ]
                  }
                ]
              },
              {
                "type": "tr",
                "children": [
                  {
                    "type": "td",
                    "children": [
                      {
                        "type": "p",
                        "children": [{ "text": "bold text", "bold": true }]
                      }
                    ]
                  },
                  {
                    "type": "td",
                    "children": [
                      {
                        "type": "p",
                        "children": [{ "text": "" }]
                      }
                    ]
                  }
                ]
              }
            ]
          }
        ]
      }
    ]
  }
]

我没有看到任何null但它删除了所有undefined

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("table", parseTable);
parsers.set("tablerow", parseRow);
parsers.set("htr", parseHTR);
parsers.set("tablehc", parseHC);
parsers.set("tbody", parseTBody);
parsers.set("btr", parseBTR);
parsers.set("tablec", parseTableC);
function convert(obj) {
  return [parsers.get(obj.nodeType)(obj)];
}
function parseDocument(obj) {
  let type = "doc";
  let children = [];
  obj.content.forEach((e) => children.push(parsers.get(e.nodeType)(e)));
  return {
    type,
    children: children.filter((e) => e !== undefined)
  };
}
function parseParagraph(obj) {
  let type = "p";
  let children = [];
  obj.content.forEach((e) => children.push(parsers.get(e.nodeType)(e)));
  return {
    type,
    children: children.filter((e) => e !== undefined)
  };
}
function parseText(obj) {
  const result = {};
  result.text = obj.value;
  obj.marks.forEach((e) => (result[e.type] = true));
  return result;
}
function parseTable(obj) {
  let type = "table";
  let children = [];
  children.push(parsers.get("tbody")(obj));
  obj.content.forEach((e) => {
    children.push(parsers.get(e.nodeType)(e));
  });
  return {
    type,
    children: children.filter((e) => e !== undefined)
  };
}
function parseRow(obj) {
  var type = [];
  let children = [];
  let check = [];
  obj.content.forEach((e) => {
    function add(item) {
      if (check.indexOf(item) === -1) {
        check.push(item);
      }
    }
    add(e.nodeType);
  });
  if (check.join() === "tablehc") {
    type.push("thead");
    children.push(parsers.get("htr")(obj));
  } else {
    // getting null and undefined from here
  }
  type = [...new Set(type)].join();
  if (children.length > 0)
    return {
      type,
      children: children.filter((e) => e !== undefined)
    };
}
function parseHTR(obj) {
  let type = "tr";
  let children = [];
  obj.content.forEach((e) => {
    children.push(parsers.get(e.nodeType)(e));
  });
  return {
    type,
    children: children.filter((e) => e !== undefined)
  };
}
function parseHC(obj) {
  let type = "th";
  let children = [];
  obj.content.forEach((e) => {
    children.push(parsers.get(e.nodeType)(e));
  });
  return {
    type,
    children: children.filter((e) => e !== undefined)
  };
}
function parseTBody(obj) {
  let type = "tbody";
  let children = [];
  children.push(parsers.get("btr")(obj));
  return {
    type,
    children: children.filter((e) => e !== undefined)
  };
}
function parseBTR(obj) {
  let type = "tr";
  let children = [];
  obj.content.forEach((e) => {
    if (e.nodeType === "tablec")
      children.push(parsers.get(e.nodeType)(e));
  });
  if (children.length > 0) return {
    type,
    children: children.filter((e) => e !== undefined)
  };
}
function parseTableC(obj) {
  let type = "td";
  let children = [];
  obj.content.forEach((e) => children.push(parsers.get(e.nodeType)(e)));
  return {
    type,
    children: children.filter((e) => e !== undefined)
  };
}
let result = convert(getSrcData());
console.log("Converted object: ", result);
function getSrcData() {
  return {
    nodeType: "document",
    content: [{
        nodeType: "paragraph",
        content: [{
          nodeType: "text",
          value: "dummy testing bold",
          marks: [{
            type: "bold",
          }, ],
        }, ],
      },
      {
        nodeType: "table",
        content: [{
            nodeType: "tablerow",
            content: [{
                nodeType: "tablehc",
                content: [{
                  nodeType: "paragraph",
                  content: [{
                    nodeType: "text",
                    value: "hey",
                    marks: [],
                  }, ],
                }, ],
              },
              {
                nodeType: "tablehc",
                content: [{
                  nodeType: "paragraph",
                  content: [{
                    nodeType: "text",
                    value: "",
                    marks: [],
                  }, ],
                }, ],
              },
            ],
          },
          {
            nodeType: "tablerow",
            content: [{
              nodeType: "tablec",
              content: [{
                nodeType: "paragraph",
                content: [{
                  nodeType: "text",
                  value: "code text",
                  marks: [{
                    type: "code",
                  }, ],
                }, ],
              }, ],
            }, ],
          },
          {
            nodeType: "tablerow",
            content: [{
              nodeType: "tablec",
              content: [{
                nodeType: "paragraph",
                content: [{
                  nodeType: "text",
                  value: "text",
                  marks: [{
                    type: "bold",
                  }, ],
                }, ],
              }, ],
            }, ],
          },
        ],
      },
      {
        nodeType: "paragraph",
        content: [{
          nodeType: "text",
          value: "",
          marks: [],
        }, ],
      },
      {
        nodeType: "table",
        content: [{
            nodeType: "tablerow",
            content: [{
                nodeType: "tablehc",
                content: [{
                  nodeType: "paragraph",
                  content: [{
                    nodeType: "text",
                    value: "hey",
                    marks: [],
                  }, ],
                }, ],
              },
              {
                nodeType: "tablehc",
                content: [{
                  nodeType: "paragraph",
                  content: [{
                    nodeType: "text",
                    value: "",
                    marks: [],
                  }, ],
                }, ],
              },
              {
                nodeType: "tablehc",
                content: [{
                  nodeType: "paragraph",
                  content: [{
                    nodeType: "text",
                    value: "",
                    marks: [],
                  }, ],
                }, ],
              },
            ],
          },
          {
            nodeType: "tablerow",
            content: [{
                nodeType: "tablec",
                content: [{
                  nodeType: "paragraph",
                  content: [{
                    nodeType: "text",
                    value: "code text",
                    marks: [{
                      type: "code",
                    }, ],
                  }, ],
                }, ],
              },
              {
                nodeType: "tablec",
                content: [{
                  nodeType: "paragraph",
                  content: [{
                    nodeType: "text",
                    value: "",
                    marks: [],
                  }, ],
                }, ],
              },
            ],
          },
          {
            nodeType: "tablerow",
            content: [{
                nodeType: "tablec",
                content: [{
                  nodeType: "paragraph",
                  content: [{
                    nodeType: "text",
                    value: "bold text",
                    marks: [{
                      type: "bold",
                    }, ],
                  }, ],
                }, ],
              },
              {
                nodeType: "tablec",
                content: [{
                  nodeType: "paragraph",
                  content: [{
                    nodeType: "text",
                    value: "",
                    marks: [],
                  }, ],
                }, ],
              },
            ],
          },
        ],
      },
    ],
  };
}

最新更新