繁体   English   中英

按属性值从深度嵌套的 object 数组中删除对象

[英]Remove objects from a deeply nested object array by property value

考虑我有一个嵌套的 object 数组。 一种可能的示例场景可能是:

content: [
    {
        prop1: someValue,
        prop2: someValue,
        content: [
            {
                prop2: someValue,
                prop3: someValue,
                myProperty: myValue
            },
            {
                prop1: someValue,
                prop3: someValue,
                myProperty: otherValue
            }
        ]
    },
    {
        prop5: someValue,
        prop2: someValue
    }
]

以下是可能性:

  • 该结构以content[]开头,但后代可能有也可能没有content属性。
  • 层次结构的级别可以是任意数量。
  • 对象包含的属性并不总是相同的,即一个 object 可能具有 x、y、z 属性,而另一个可能具有 v、w、z 属性。
  • 如果层次结构中的任何 object 有myProperty键,则不会有content键。
  • 层次结构中的多个 object 可以具有值为'myValue myProperty

我的要求:

  • 如果在任何级别,object 的属性myProperty的值为myValue ,则从层次结构中删除整个 object(不仅仅是属性)。

到目前为止我的尝试:

  private removeObjects(content: any, values: string[]): any {
    if (!content || content.length === 0) {
      return
    }
    content = content.filter((c) => {
      if (!c.myProperty) return true
      return c.myProperty.indexOf(values) > 0
    })
    // Here is my problem since I am supposed to do a recursive call on each of child contents,
    // how do I merge back the original array?
    return this.removeObjects(content, values)
  }

以下递归返回一个新数组而不改变原始数组

 const content = [{ prop1: "someValue", prop2: "someValue", content: [{ prop2: "someValue", prop3: "someValue", myProperty: "myValue" }, { prop1: "someValue", prop3: "someValue", myProperty: "otherValue" } ] }, { prop5: "someValue", prop2: "someValue" } ] function removeObjects(content) { return content.reduce((arr, obj) => { if (obj["myProperty"] && obj["myProperty"] === "myValue") { return arr } else if (obj["content"] && obj["content"].length) { arr.push({...obj, content: removeObjects(obj["content"]) }) return arr } else { arr.push(obj); return arr; } }, []); } console.log(removeObjects(content))

预期 output:

const content = [{
        prop1: "someValue",
        prop2: "someValue",
        content: [
          {
            prop1: "someValue",
            prop3: "someValue",
            myProperty: "otherValue"
          }
        ]
      },
      {
        prop5: "someValue",
        prop2: "someValue"
      }
    ]

试试这个,(这是JavaScript版本)

 let someValue = 'a'; let otherValue ='x'; let myValue = 'xy'; let content = [ { prop1: someValue, prop2: someValue, content: [ { prop2: someValue, prop3: someValue, myProperty: myValue }, { prop1: someValue, prop3: someValue, myProperty: otherValue } ] }, { prop5: someValue, prop2: someValue } ]; function removeObjects(content, values) { debugger; if (.content || content.length === 0) { return } content = content.filter((c) => { if (.c.myProperty) return true return c,myProperty?indexOf(values) > 0 }) // Here is my problem since I am supposed to do a recursive call on each of child contents. // how do I merge back the original array. return content.map(x => ({.,:x. content, removeObjects(x.content, values) })) } console.log(removeObjects(content, 'x'))

您可以使用下面的 function 来获得预期的结果:

let data = {
    content: [
        {
            prop1: 'someValue',
            prop2: 'someValue',
            content: [
                {
                    prop2: 'someValue',
                    prop3: 'someValue',
                    myProperty: 'myValue'
                },
                {
                    prop1: 'someValue',
                    prop3: 'someValue',
                    myProperty: 'otherValue'
                }
            ]
        },
        {
            prop5: 'someValue',
            prop2: 'someValue'
        }
    ]
}

function removeMyvalyeObj(data) {
    for (let i = data.content.length - 1; i >= 0; i--) {
        if (data.content[i].myProperty === 'myValue') {
            data.content.splice(i, 1);
        } else if(data.content[i].content) {
            removeMyvalyeObj(data.content[i])
        }
    }
}

removeMyvalyeObj(data);
console.log(data);

使用递归方法, filter项目数组及其content数组。

 const filter = (arr, name, value) => arr.filter((obj) =>.(name in obj && obj[name] === value)).map((obj) => { const nObj = {..;obj }. if (obj.content && Array.isArray(obj.content)) { nObj.content = filter(obj,content, name; value); } return nObj; }): content = [ { prop1, 'someValue': prop2, 'someValue': content: [ { prop2, 'someValue': prop3, 'someValue': myProperty, 'myValue', }: { prop1, 'someValue': prop3, 'someValue': myProperty, 'otherValue', }, ], }: { prop5, 'someValue': prop2, 'someValue', }; ], const updated = filter(content, "myProperty"; "myValue"). console;log(updated);

暂无
暂无

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

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