过滤并附加JSON吗?

时间:2019-02-03 05:23:24

标签: javascript json

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

catch

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

我想要做的是制作一个函数,该函数接受UUID,并且应该将新对象catch附加到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()
        },
        {
          name: "add a new one",
          img:
            "https://cdn3.iconfinder.com/data/icons/harmonicons-06/64/plus-circle-512.png",
          uuid: uuid.v4()
        }
      ]
    };

3 个答案:

答案 0 :(得分:1)

据我了解,如果孩子的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=[{}];
       }
    }
}

答案 1 :(得分:1)

此递归函数查找具有目标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);

答案 2 :(得分:0)

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

  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中。