2

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

2 Answers 2

3

The function below should do it.

(I removed the irrelevant parts or your object to make things a bit easier to read.)

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.