Home > Back-end >  How to remove null or undefined from the return value in node js / javascript
How to remove null or undefined from the return value in node js / javascript

Time:11-08

I wrote a code and I want to remove undefined or null value from here which I am returning

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: [],
                      },
                    ],
                  },
                ],
              },
            ],
          },
        ],
      },
    ],
  };
}

Is there any possible way I can get input value without this undefined and null value

I am getting undefined here but in my system I am getting null value in place of undefined is there any possible if I can return with out the null value and undefined in my actual output

as my expected value is like this

expected output:-

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": "" }]
                      }
                    ]
                  }
                ]
              }
            ]
          }
        ]
      }
    ]
  }
]

CodePudding user response:

I don't see any nulls but it removes every 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: [],
                  }, ],
                }, ],
              },
            ],
          },
        ],
      },
    ],
  };
}

  • Related