【问题标题】:How do we remove duplicate from array. Remove the first object at Top level javascript我们如何从数组中删除重复项。删除顶级 javascript 的第一个对象
【发布时间】:2021-12-20 20:56:28
【问题描述】:

如何从根目录中删除重复项。基本上我想查看 UUID 属性的完整数组。如果在数组中的其他任何位置找到来自数组根的 UUID,我想从根中删除该对象。

示例:“UUID”:“9E741BEC-7BC1-49C0-8214-AB5A65762CB7”位于树的顶层。它也在其中一个孩子身上发现。所以我想删除顶层的on

{
  "UUID": "9E741BEC-7BC1-49C0-8214-AB5A65762CB7",
  "Visible": false,
  "WasVisibleTransient": false,
  "WhichCorners": 15,
  "Width": 796.5216064453125,
  "Width2": 796.5216064453125,
  "Width3": 796.5216064453125,
  "title": "Frances Stowe Great lesson ????"
  },
  {
    "UUID": "9E741BEC-7BC",
    "Visible": false,
    "WasVisibleTransient": false,
    "WhichCorners": 15,
    "Width": 796.5216064453125,
    "Width2": 796.5216064453125,
    "Width3": 796.5216064453125,
    "title": "Nice to know, 
    "Children": [{
      "UUID": "9E741BEC-7BC1-49C0-8214-AB5A65762CB7",
      "Visible": false,
      "WasVisibleTransient": false,
      "WhichCorners": 15,
      "Width": 796.5216064453125,
      "Width2": 796.5216064453125,
      "Width3": 796.5216064453125,
      "title": "Frances Stowe Great lesson ????",
      "Children": [{
        "UUID": "9E741",
        "Visible": false,
        "WasVisibleTransient": false,
        "WhichCorners": 15,
        "Width": 796.5216064453125,
        "Width2": 796.5216064453125,
        "Width3": 796.5216064453125,
        "title": "Frances Stowe Great lesson ????"
      }],
    }],
  },
  {
    "UUID": "9E741BEC-7BC",
    "Visible": false,
    "WasVisibleTransient": false,
    "WhichCorners": 15,
    "Width": 796.5216064453125,
    "Width2": 796.5216064453125,
    "Width3": 796.5216064453125,
    "title": "Nice to know
  },
},

我想要的输出如下

{
      "UUID": "9E741BEC-7BC",
      "Visible": false,
      "WasVisibleTransient": false,
      "WhichCorners": 15,
      "Width": 796.5216064453125,
      "Width2": 796.5216064453125,
      "Width3": 796.5216064453125,
      "title": "Nice to know, 
      "Children": [{
        "UUID": "9E741BEC-7BC1-49C0-8214-AB5A65762CB7",
        "Visible": false,
        "WasVisibleTransient": false,
        "WhichCorners": 15,
        "Width": 796.5216064453125,
        "Width2": 796.5216064453125,
        "Width3": 796.5216064453125,
        "title": "Frances Stowe Great lesson ????",
        "Children": [{
          "UUID": "9E741",
          "Visible": false,
          "WasVisibleTransient": false,
          "WhichCorners": 15,
          "Width": 796.5216064453125,
          "Width2": 796.5216064453125,
          "Width3": 796.5216064453125,
          "title": "Frances Stowe Great lesson ????"
        }],
      }],
    },
    {
      "UUID": "9E741BEC-7BC",
      "Visible": false,
      "WasVisibleTransient": false,
      "WhichCorners": 15,
      "Width": 796.5216064453125,
      "Width2": 796.5216064453125,
      "Width3": 796.5216064453125,
      "title": "Nice to know
    },
  },

【问题讨论】:

    标签: javascript arrays filter


    【解决方案1】:

    你可以试试这个:

    function removeRootUUID(baseArray, currArray) {
        const delInRoot = (uuidRef) => {
            baseArray.forEach( (obj, i) => {
                if (obj.UUID === uuidRef) {
                    baseArray.splice(i, 1);
                }
            });
        }
        for (const obj of currArray) {
            if (obj.Children) {
                obj.Children.forEach( cObj => {
                    delInRoot(cObj.UUID);
                })
                removeRootUUID(baseArray, obj.Children);
            }
        };
    }
    
    // arr is your initial array here
    removeRootUUID(arr, arr);
    console.log(arr);
    

    我相信它可能会更好,但它适用于您的示例

    【讨论】:

    • 所以会是这样:removeRootUUID(response.data.items, response.data.items); console.log(JSON.stringify(response.data.items));
    • @Jerryseigle 是的,我猜
    • 好吧,我试过了,但只是打印了原始数组
    • @Jerryseigle 在我这边,第一个对象被删除;您的响应对象是否为以下形式:{ data: { items: [ {}, {}, ... ] } }
    • 数据结构发生了变化,但是每个对象中总是有一个 UUID 和 Children 属性
    猜你喜欢
    • 2020-10-25
    • 1970-01-01
    • 2021-07-24
    • 2018-11-14
    • 1970-01-01
    • 2016-07-02
    • 1970-01-01
    • 2021-12-30
    相关资源
    最近更新 更多