首页 > 解决方案 > How to remove multi nested object in javascript using id

问题描述

Declaration of id

var id = 3;

I want to update this object

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 is this

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

Function Object is this

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

I want output like this

          {"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"
        }
      }

Any help would be appreciated

Note: I want to remove nested subcomments object using id if I pass id=3 then it should remove subcomment of 2, How can I remove subcomments of 2 if id = 3

标签: javascriptobject

解决方案


下面的功能应该做到这一点。

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

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);


推荐阅读