0

I have the following object:

const jj = {
    "type": "doc",
    "content": [{
        "type": "paragraph",
        "content": [{
            "type": "text",
            "text": "HI  \n"
        }, {
            "type": "text",
            "marks": [{
                "type": "link",
                "attrs": {
                    "href": "https://infosysta-my.sharepoint.com/:x:/p/elie%5Fiskandar/EXmik99OQBBNiJua1zbbiUIBwU8QEVOzyXl3qRV3ZnhADw",
                    "__confluenceMetadata": null
                }
            }],
            "text": "Client List - MW.xlsx"
        }]
    }, {
        "type": "paragraph",
        "content": [{
            "type": "text",
            "text": "regards,"
        }]
    }],
    "version": 1
};

I need to remove the "__confluenceMetadata":null key/value so the new object will be:

const jj = {
    "type": "doc",
    "content": [{
        "type": "paragraph",
        "content": [{
            "type": "text",
            "text": "HI  \n"
        }, {
            "type": "text",
            "marks": [{
                "type": "link",
                "attrs": {
                    "href": "https://infosysta-my.sharepoint.com/:x:/p/elie%5Fiskandar/EXmik99OQBBNiJua1zbbiUIBwU8QEVOzyXl3qRV3ZnhADw"
                }
            }],
            "text": "Client List - MW.xlsx"
        }]
    }, {
        "type": "paragraph",
        "content": [{
            "type": "text",
            "text": "regards,"
        }]
    }],
    "version": 1
};

I tired to iterate through the elements using let arr = Object.entries(ADFDocJson); but I couldn't reach what I needed.

I am looking for a generic method to use as this is just an example object and there might be multiple objects in the marks array.

D M
  • 5,769
  • 4
  • 12
  • 27
Sora
  • 2,465
  • 18
  • 73
  • 146
  • 1
    Does this answer your question? [How do I remove a property from a JavaScript object?](https://stackoverflow.com/questions/208105/how-do-i-remove-a-property-from-a-javascript-object) – Pete May 31 '22 at 13:47
  • not working for me – Chris G May 31 '22 at 13:53

1 Answers1

1

Presented below is one possible way to achieve the desired objective.

Code Snippet

const removeMeta = argObj => (
  Object.fromEntries(
    Object.entries(argObj)
    .map(([k, v]) => (
      k !== 'content'
      ? ([k, v])
      : ([k, (
        v.map(ob1 => (
          !('content' in ob1)
          ? ({...ob1})
          : ({...ob1,
            content: ob1.content.map(
              ob2 => (
                !('marks' in ob2)
                ? ({ ...ob2 })
                : ({ ...ob2,
                  marks: ob2.marks.map(ob3 => (
                    !('attrs' in ob3 &&
                    '__confluenceMetadata' in ob3.attrs &&
                    ob3.attrs['__confluenceMetadata'] === null
                    )
                    ? ({ ...ob3 })
                    : ({ ...ob3, 
                      attrs: Object.fromEntries(
                        Object.entries(ob3.attrs)
                        .filter(
                          (
                            [k2, v2]
                          ) => (
                            k2 !== '__confluenceMetadata'
                          )
                        )
                      )
                    })
                  ))
                })
              )
            )
          })
        ))
      )])
    ))
  )
);
const jj = {
    "type": "doc",
    "content": [{
        "type": "paragraph",
        "content": [{
            "type": "text",
            "text": "HI  \n"
        }, {
            "type": "text",
            "marks": [{
                "type": "link",
                "attrs": {
                    "href": "https://infosysta-my.sharepoint.com/:x:/p/elie%5Fiskandar/EXmik99OQBBNiJua1zbbiUIBwU8QEVOzyXl3qRV3ZnhADw",
                    "__confluenceMetadata": null
                }
            }],
            "text": "Client List - MW.xlsx"
        }]
    }, {
        "type": "paragraph",
        "content": [{
            "type": "text",
            "text": "regards,"
        }]
    }],
    "version": 1
};

console.log(removeMeta(jj));
.as-console-wrapper { max-height: 100% !important; top: 0 }

Explanation

Iterate over the various key-value pairs of the object, and then iterate over the nested 'content' arrays, identify the relevant prop and filter it out.

jsN00b
  • 3,584
  • 2
  • 8
  • 21