繁体   English   中英

过滤并附加JSON吗?

[英]Filtering and appending in JSON?

我在React中有一个JSON变量,如下所示:

var newTreeData_2 = {
      name: current_name,
      img: current_img,
      uuid: uuid.v4(),
      children: [
        {
          name: "Edit and save",
          img:
            "https://cdn3.iconfinder.com/data/icons/harmonicons-06/64/plus-circle-512.png",
          uuid: uuid.v4()
        },
        {
          name: "add a new one",
          img:
            "https://cdn3.iconfinder.com/data/icons/harmonicons-06/64/plus-circle-512.png",
          uuid: uuid.v4()
        }
      ]
    };

每个uuid.v4()是唯一的ID。

我要做的是制作一个接受UUID的函数,并将一个新的对象children:[{}]附加到UUID所在的位置。 例如,如果我的uuid与代码段第10行上的uuid.v4()相匹配,则它应该附加一个JSON对象,因此最终结果如下所示:

  var newTreeData_2 = {
          name: current_name,
          img: current_img,
          uuid: uuid.v4(),
          children: [
            {
              name: "Edit and save",
              img:
                "https://cdn3.iconfinder.com/data/icons/harmonicons-06/64/plus-circle-512.png",
              uuid: uuid.v4(), 
              chilren: [{}]

            },
            {
              name: "add a new one",
              img:
                "https://cdn3.iconfinder.com/data/icons/harmonicons-06/64/plus-circle-512.png",
              uuid: uuid.v4()
            }
          ]
        };

据我了解,如果孩子的uuid等于给定的uuid,则想添加一个对象。

如果这是您想要的,则可以遍历每个孩子,测试它的uuid是否等于给定的uuid,如果是,则添加[{}]

function addJSONObjct(obj, uuid) {
    for (i = 0; i < obj.children.length; i ++) {
       if (obj.children[i].uuid === uuid) {
            obj.children[i].children=[{}];
       }
    }
}

此递归函数查找具有目标UUID的对象并将子代添加到其中

 var newTreeData = { name: 'a', img: 'b', uuid: 1234, children: [{ name: "Edit and save", img: "https://cdn3.iconfinder.com/data/icons/harmonicons-06/64/plus-circle-512.png", uuid: 12345, children: [{ name: "Edit and save", img: "https://cdn3.iconfinder.com/data/icons/harmonicons-06/64/plus-circle-512.png", uuid: 123 }] }, { name: "add a new one", img: "https://cdn3.iconfinder.com/data/icons/harmonicons-06/64/plus-circle-512.png", uuid: 132456 } ] }; function findUUID(targetObject, uuid) { var children = targetObject.children; if (!!children === false) return; var target = children.filter(x => { if (x.uuid === uuid) return x; }); if (target.length === 0) { children.map(x => findUUID(x, uuid)); } else { if (!!target[0].children && target[0].children.length !== 0) { target[0].children.push({}); console.log(target); } else { target[0].children = [{}]; console.log(target); } } } findUUID(newTreeData, 132456); 

我必须解决此问题的确切代码:

  findUUID(targetObject, uuid2, name, img, details) {
var targetIsFound = false;
var target = "";
console.log("Parent TreeData UUID" + targetObject.uuid);
console.log("UUID we are trying to match" + uuid2);
if (targetObject.uuid == uuid2) {
  targetIsFound = true;
  target = targetObject;
}

console.log(targetIsFound, target);
if (targetIsFound == false) {
  console.log(
    "About to start recursion, this is the target Object: ",
    targetObject
  );
  if (targetObject.children === undefined) {
    console.log("we should exit here");
  } else {
    targetObject.children.map(x => this.findUUID(x, uuid2));
  }
} else {
  if (target.name == "Edit and save") {
    target.children = [
      {
        name: "Edit and save",
        img:
          "https://cdn3.iconfinder.com/data/icons/harmonicons-06/64/plus-circle-512.png",
        uuid: uuid.v4()
      },
      {
        name: "Edit and save",
        img:
          "https://cdn3.iconfinder.com/data/icons/harmonicons-06/64/plus-circle-512.png",
        uuid: uuid.v4()
      }
    ];
  }
  this.setState({ treeData: this.state.treeData });
  console.log(this.state.treeData);
}
}

名称,img和详细信息仅是用于以匹配的uuid填充数据的参数。 我将匹配的uuid命名为“ uuid2”的原因是因为我导入了uuid程序包,并且可能存在冲突。

Jeff和Barzin的代码有效,如果您的数据像我的一样,则必须对Jeff的方法运行递归。 我的代码与Barzin的代码不同之处在于,我正在执行错误检查:if(targetObject.children === undefined)以确保没有其他可执行的操作时不继续执行递归。 顺便说一句,此方法假设我们要查找的UUID确实存在于targetObject中。

暂无
暂无

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

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