简体   繁体   English

从位于父元素内的对象中删除特定元素

[英]Removing a specific element from an object that is located inside a parent element

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:我需要删除"__confluenceMetadata":null键/值,所以新对象将是:

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);我厌倦了使用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.我正在寻找一种通用方法,因为这只是一个示例对象,并且marks数组中可能有多个对象。

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.遍历对象的各种键值对,然后遍历嵌套的“内容”数组,识别相关的道具并将其过滤掉。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM