簡體   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