过滤和JSON追加?

问题描述 投票:0回答:3

我有一个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()
            }
          ]
        };
javascript json
3个回答
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);

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=[{}];
       }
    }
}

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封装,可以有冲突。

杰夫和Barzin的代码工作,如果你的数据是像我这样的,你必须运行在杰夫的方法递归。我的代码是从Barzin的不同之处在于我做了错误检查:如果(targetObject.children ===未定义),以确保我不要继续,如果有什么与执行它来执行递归。顺便说一句,这种方法使我们正在努力寻找的UUID在targetObject肯定存在的假设。

© www.soinside.com 2019 - 2024. All rights reserved.