简体   繁体   English

过滤并附加JSON吗?

[英]Filtering and appending in JSON?

I have a JSON variable in React like this: 我在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()
        }
      ]
    };

Each uuid.v4() is a unique ID. 每个uuid.v4()是唯一的ID。

What I want to do is to make a function which takes in a UUID, and should append a new object children:[{}] to where the UUID is located at. 我要做的是制作一个接受UUID的函数,并将一个新的对象children:[{}]附加到UUID所在的位置。 For example, if **my uuid matches the uuid.v4() on line 10 of the code snippet, it should append a JSON object so the end result looks like: 例如,如果我的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()
            }
          ]
        };

As I understand it, you want to add an object if the child's uuid is equal to a given uuid. 据我了解,如果孩子的uuid等于给定的uuid,则想添加一个对象。

If that's what you want, you can loop through each child, test if it's uuid is equal to the given uuid, and if it is, add [{}] . 如果这是您想要的,则可以遍历每个孩子,测试它的uuid是否等于给定的uuid,如果是,则添加[{}]

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

this recursive function find object with target UUID and add children to them 此递归函数查找具有目标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); 

The exact code i have to solve this problem: 我必须解决此问题的确切代码:

  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);
}
}

name, img and details are just parameters to fill the data with the uuids matched. 名称,img和详细信息仅是用于以匹配的uuid填充数据的参数。 The reason I named the matching uuid "uuid2" is because I imported the uuid package and there can be conflicts. 我将匹配的uuid命名为“ uuid2”的原因是因为我导入了uuid程序包,并且可能存在冲突。

Jeff and Barzin's code work, If your data is like mine you have to run recursion on Jeff's method. Jeff和Barzin的代码有效,如果您的数据像我的一样,则必须对Jeff的方法运行递归。 My code is different from Barzin's in that I am doing an error check: if(targetObject.children === undefined) to make sure I don't continue to perform recursion if there is nothing to perform it with. 我的代码与Barzin的代码不同之处在于,我正在执行错误检查:if(targetObject.children === undefined)以确保没有其他可执行的操作时不继续执行递归。 By the way, this method puts assumption that the UUID we are trying to find definitely exists in targetObject. 顺便说一句,此方法假设我们要查找的UUID确实存在于targetObject中。

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

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