代码之家  ›  专栏  ›  技术社区  ›  Acy

基于属性删除JSON对象

  •  1
  • Acy  · 技术社区  · 7 年前

    var newTreeData = {
            name: submitted_title,
            resource_link: submitted_resource_link,
            details: submitted_details,
            uuid: submitted_uuid,
            website_image: submitted_website_img,
            children: [
              {
                name: "Edit and save",
                resource_link: "uh",
                uuid: uuid.v4(),
                details: "hi",
                website_image:
                  "https://cdn3.iconfinder.com/data/icons/harmonicons-06/64/plus-circle-512.png",
                children: [{...}, {}, ...]
              },
    
    
    
    
              {
                name: "Edit and save",
                resource_link: "uh",
                uuid: uuid.v4(),
                details: "hi",
                website_image:
                  "https://cdn3.iconfinder.com/data/icons/harmonicons-06/64/plus-circle-512.png"
              }
            ]
          };
    

    线路 children: [{...}, {}] 只是表示newTreeData的孩子可以有孩子,而这可以有孩子。。。

    无论如何,我写了一个方法名 Finduuidthendelite 在伪代码中应该执行哪些操作: if(object.uuid == toFindUUID) then delete object

      findUUIDthenDelete = (orig_data, to_delete_uuid) => {
         var targetIsFound = false;
         if (orig_data.uuid == to_delete_uuid) {
            targetIsFound = true;
         }
    
         if (targetIsFound == false) {
            if (orig_data.children === undefined) {
         } else {
        //if target not found, run recursion
           orig_data.children.map(eachChildren =>
             this.findUUIDthenDelete(eachChildren, to_delete_uuid)
            );
          }
        } else {
          console.log(orig_data, "this is the child ");
          console.log(orig_data.parent, "is found, deleting its parent");
          delete orig_data
    
        } 
      };
    

    正如您所看到的,该方法分为两部分:首先,我找到了具有我们试图寻找的uuid的对象(可能使用一些递归),然后删除该对象。然而,现在我得到了“在局部变量严格模式下删除”的错误,因为这样做 delete orig_data

    2 回复  |  直到 7 年前
        1
  •  3
  •   ChrisG    7 年前

    这应该做到:

    function findUUIDthenDelete(tree, uuid) {
      if (!tree.children) return;
      tree.children = tree.children.filter(c => c.uuid !== uuid);
      tree.children.forEach(c => findUUIDthenDelete(c, uuid));
    }
    

    应该是不言自明的。 首先,如果当前节点没有子节点,请立即退出。
    uuid 匹配使用 filter() .
    最后是递归。

        2
  •  1
  •   flaw13    7 年前

    const testMap = Immutable.fromJS({
      uuid: 1,
      children: [{
        uuid: 2,
        children: [{
            uuid: 3,
            children:[{
              uuid: 8
              }]
          },
          {
            uuid: 4
          },
          {
            uuid: 5
          },
        ]
      },
      {
        uuid: 7
      }]
    });
    
    function findPath(checkMap, uuid, pathMap, currentIndex) {
      if (checkMap.has('uuid') && checkMap.get('uuid') === uuid) {
        const updatePathMap = pathMap.get('path').push(currentIndex);
        return new Immutable.Map({
          found: true,
          path: pathMap.get('path').push(currentIndex)
        });
      } else {
        if (checkMap.has('children') && checkMap.get('children').size > 0) {
          for (let i = 0; i < checkMap.get('children').size; i++) {
            const child = checkMap.get('children').get(i);
            const checkChildPath = findPath(child, uuid, pathMap, i);
            if (checkChildPath.get('found') === true) {
              let updatePath =  checkChildPath.get('path').push('children');
              updatePath = updatePath.push(currentIndex);
              return new Immutable.Map({
                found: true,
                path: updatePath
              });
            }
          }
        }
        return pathMap;
      }
    }
    
    const testPath = findPath(testMap, 7, new Immutable.Map({
      found: false,
      path: new Immutable.List()
    }), 0);
    
    console.info(testPath);
    
    const testPath2 = findPath(testMap, 8, new Immutable.Map({
      found: false,
      path: new Immutable.List()
    }), 0);
    
    console.info(testPath2);
    
    if (testPath2.get('found') === true) {
    
      const path = testPath2.get('path');
      if (path.size === 1 && path.get(0) === 0) {
        // Your highlest level map has the uuid
      } else {
        const truePath = path.shift();
        const cleanedUpMap = testMap.removeIn(truePath);
        console.info(cleanedUpMap);
      }
        
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.8.2/immutable.js"></script>