繁体   English   中英

如何使用ID在JavaScript中删除多嵌套对象

[英]How to remove multi nested object in javascript using id

身份证声明

var id = 3;

我想更新这个对象

var obj = {"comments" : {
                "commentedBy" : "test",
                "date" : "",
                "comment" : "Hello world",
                "subComments" : {
                    "commentedBy" : "jaril 2",
                    "date" : "",
                    "comment" : "Hello world inside dark",
                    "subComments" : {
                        "commentedBy" : "jaril 3",
                        "date" : "",
                        "comment" : "wow working great",
                        "subComments" : {
                            "commentedBy" : "jaril 4",
                            "date" : "",
                            "comment" : "wow working great",
                            "commentId" : 4
                        },
                        "commentId" : 3
                    },
                    "commentId" : 2
                },
                "commentId" : 1
            },
            "dueDate" : "",
            "createdDate" : "",
            "lastUpdated" : "",
            "checkList" : [],
            "position" : 2,
            "status" : "active"
        }
      }

功能是这个

function deleteCommentId(comments){
  if (comments.commentId == id)){
    delete comments;
    return comments;
  }
  if (comments.subComments) {
    deleteCommentId(comments.subComments);
  } 
  return comments;
}

功能对象是这个

if(id == 1){
    result[0].comments = {};
} else {
    deleteCommentId(obj.comments);
}
console.log("final object==>", obj);

我想要这样的输出

          {"comments" : {
                "commentedBy" : "test",
                "date" : "",
                "comment" : "Hello world",
                "subComments" : {
                    "commentedBy" : "jaril 2",
                    "date" : "",
                    "comment" : "Hello world inside dark",
                    "commentId" : 2
                },
                "commentId" : 1
            },
            "dueDate" : "",
            "createdDate" : "",
            "lastUpdated" : "",
            "checkList" : [],
            "position" : 2,
            "status" : "active"
        }
      }

任何帮助,将不胜感激

注意:如果我通过id = 3,我想使用id删除嵌套的子注释对象,那么它应该删除2的子注释,如果id = 3,如何删除2的子注释。

下面的功能可以做到这一点。

(我删除了不相关的部分或您的对象,使内容更易于阅读。)

 var obj = { "comments": { "subComments": { "subComments": { "subComments": { "commentId": 4 }, "commentId": 3 }, "commentId": 2 }, "commentId": 1 } }; function deleteCommentId(comments, id) { if (comments.subComments) { if (comments.subComments.commentId === id) { delete comments.subComments; } else { deleteCommentId(comments.subComments, id); } } } deleteCommentId(obj.comments, 3); console.log(obj); 

 var id = 3; var obj = {"comments":{"commentedBy":"test","date":"","comment":"Hello world","subComments":{"commentedBy":"jaril 2","date":"","comment":"Hello world inside dark","subComments":{"commentedBy":"jaril 3","date":"","comment":"wow working great","subComments":{"commentedBy":"jaril 4","date":"","comment":"wow working great","commentId":4},"commentId":3},"commentId":2},"commentId":1},"dueDate":"","createdDate":"","lastUpdated":"","checkList":[],"position":2,"status":"active"} function deleteCommentId(comments, id) { if (comments.subComments) { if (comments.subComments.commentId === id) { delete comments.subComments; } else { deleteCommentId(comments.subComments, id); } } } deleteCommentId(obj.comments, id); console.log("final object==>",obj); var expectedJSON = {"comments":{"commentedBy":"test","date":"","comment":"Hello world","subComments":{"commentedBy":"jaril 2","date":"","comment":"Hello world inside dark","commentId":2},"commentId":1},"dueDate":"","createdDate":"","lastUpdated":"","checkList":[],"position":2,"status":"active"} console.log("Output match: ",JSON.stringify(obj) == JSON.stringify(expectedJSON)); 

暂无
暂无

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

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