Javascript 在JSON中过滤和附加?

Javascript 在JSON中过滤和附加?,javascript,json,Javascript,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/har

我在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的函数,并在UUID所在的位置追加一个新对象
子对象:[{}]
。例如,如果**my 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=[{}];
       }
    }
}
函数addjsonobject(obj,uuid){
对于(i=0;i
据我所知,如果子对象的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=[{}];
       }
    }
}
函数addjsonobject(obj,uuid){
对于(i=0;i
此递归函数用于查找具有目标UUID的对象并向其添加子对象

var newTreeData={
名称:‘a’,
img:‘b’,
uuid:1234,
儿童:[{
名称:“编辑并保存”,
img:“https://cdn3.iconfinder.com/data/icons/harmonicons-06/64/plus-circle-512.png",
uuid:12345,
儿童:[{
名称:“编辑并保存”,
img:“https://cdn3.iconfinder.com/data/icons/harmonicons-06/64/plus-circle-512.png",
uuid:123
}]
},
{
名称:“添加一个新的”,
img:“https://cdn3.iconfinder.com/data/icons/harmonicons-06/64/plus-circle-512.png",
uuid:132456
}
]
};
函数findUUID(targetObject,uuid){
var children=targetObject.children;
if(!!children==false)返回;
var target=children.filter(x=>{
如果(x.uuid==uuid)返回x;
});
如果(target.length==0){
map(x=>findUUID(x,uuid));
}否则{
if(!!target[0]。children&&target[0]。children.length!==0){
目标[0]。子对象。推送({});
控制台日志(目标);
}否则{
目标[0]。子项=[{}];
控制台日志(目标);
}
}
}

findUUID(newTreeData,132456)此递归函数查找具有目标UUID的对象并向其添加子对象

var newTreeData={
名称:‘a’,
img:‘b’,
uuid:1234,
儿童:[{
名称:“编辑并保存”,
img:“https://cdn3.iconfinder.com/data/icons/harmonicons-06/64/plus-circle-512.png",
uuid:12345,
儿童:[{
名称:“编辑并保存”,
img:“https://cdn3.iconfinder.com/data/icons/harmonicons-06/64/plus-circle-512.png",
uuid:123
}]
},
{
名称:“添加一个新的”,
img:“https://cdn3.iconfinder.com/data/icons/harmonicons-06/64/plus-circle-512.png",
uuid:132456
}
]
};
函数findUUID(targetObject,uuid){
var children=targetObject.children;
if(!!children==false)返回;
var target=children.filter(x=>{
如果(x.uuid==uuid)返回x;
});
如果(target.length==0){
map(x=>findUUID(x,uuid));
}否则{
if(!!target[0]。children&&target[0]。children.length!==0){
目标[0]。子对象。推送({});
控制台日志(目标);
}否则{
目标[0]。子项=[{}];
控制台日志(目标);
}
}
}

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);
}
}
name、img和details只是用匹配的uuid填充数据的参数。我之所以将匹配的uuid命名为“uuid2”,是因为我导入了uuid包,可能存在冲突


Jeff和Barzin的代码是有效的,如果您的数据与我的一样,您必须在Jeff的方法上运行递归。我的代码与Barzin的不同之处在于,我正在执行一个错误检查:if(targetObject.children===undefined),以确保在没有任何可执行的递归时不会继续执行递归。顺便说一下,这种方法假设我们试图找到的UUID肯定存在于targetObject中

我要解决这个问题的确切代码是:

  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和details只是用匹配的uuid填充数据的参数。我之所以将匹配的uuid命名为“uuid2”,是因为我导入了uuid包,可能存在冲突


Jeff和Barzin的代码是有效的,如果您的数据与我的一样,您必须在Jeff的方法上运行递归。我的代码与Barzin的不同之处在于,我正在执行一个错误检查:if(targetObject.children===undefined),以确保在没有任何可执行的递归时不会继续执行递归。顺便说一下,这种方法假设我们试图找到的UUID肯定存在于targetObject中

嗨,巴津!感谢您的回答,但有一个漏洞我无法用这个解决方案来填补:我可以在一个子项中有多个对象,只有一个子项具有匹配的UUID,但这意味着FindUID仍然会触发那些注定不具有的UUID,这将很麻烦,因为我们没有要映射的.children。你对如何解决这个问题有见解吗?我将使用if targetObject.children==undefined来查看我在这里可以做什么。无论如何,非常感谢!还竖起大拇指使用递归!上一次使用时,我还在教室里,哇!嗨,巴津!感谢您的回答,但有一个漏洞我无法用这个解决方案来填补:我可以在一个子项中有多个对象,只有一个子项具有匹配的UUID,但这意味着FindUID仍然会触发那些注定不具有的UUID,这将很麻烦,因为我们没有要映射的.children。你对如何解决这个问题有见解吗?我要用