使用唯一键更新Javascript对象的嵌套值

时间:2018-08-02 11:59:28

标签: javascript

我想在每个子注释中添加唯一的id,注释可能有100多个,我将此对象作为输入,并且必须添加自动增量并插入数据库中,我们将不胜感激

我想更新此object

var objData =   {"comments":{
        "commentedBy" : "jaril1",
        "date" : "",
        "comment" : "Hello world",
        "subComments" : {
            "commentedBy" : "jaril 2",
            "date" : "",
            "comment" : "Hello world inside dark",
            "subComments" :{
                "commentedBy": "jaril 3",
                "date": "",
                "comment": "wow working great"
            }
        }
    }
}

为此:

var objData =   {"comments":{
        "commentId":1,
        "commentedBy" : "jaril1",
        "date" : "",
        "comment" : "Hello world",
        "subComments" : {
            "commentId":2,
            "commentedBy" : "jaril 2",
            "date" : "",
            "comment" : "Hello world inside dark",
            "subComments" :{
                "commentId":3,
                "commentedBy": "jaril 3",
                "date": "",
                "comment": "wow working great"
            }
        }
    }
}

4 个答案:

答案 0 :(得分:2)

var objData = {"comments":{"commentedBy":"jaril1","date":"","comment":"Hello world","subComments":{"commentedBy":"jaril 2","date":"","comment":"Hello world inside dark","subComments":{"commentedBy":"jaril 3","date":"","comment":"wow working great"}}}}

var id=1
function updateComment(commenObj){
  return commenObj.subComments ? {...commenObj, commentId: id++, subComments: updateComment(commenObj.subComments)} : {...commenObj, commentId: id++}
}

console.log(updateComment(objData.comments))

答案 1 :(得分:1)

如果不需要额外的增量,则可以使用递归来实现,因为您的数据结构是递归的。

您定义了一个像withId_这样的递归函数,该函数接受一个对象和id,递增id,然后在subComments属性(如果存在)上调用自身:

const objData ={"comments":{
        "commentedBy" : "jaril1",
        "date" : "",
        "comment" : "Hello world",
        "subComments" : {
            "commentedBy" : "jaril 2",
            "date" : "",
            "comment" : "Hello world inside dark",
            "subComments" :{
                "commentedBy": "jaril 3",
                "date": "",
                "comment": "wow working great"
            }
        }
    }
}

const withId_ = ({ commentedBy, date, comment, subComments }, id) => {
  if (typeof subComments === 'undefined') {
    return { commentId: id + 1, commentedBy, date, comment };
 } else {
    let incrementedId = id + 1;
    return { commentId: incrementedId, commentedBy, date, comment, subComments: withId_(subComments, incrementedId) };
 }
};

const withId = (objectData) => withId_(objectData.comments, 0);

console.log(withId(objData));

输出:

{
    "commentId": 1,
    "commentedBy": "jaril1",
    "date": "",
    "comment": "Hello world",
    "subComments": {
        "commentId": 2,
        "commentedBy": "jaril 2",
        "date": "",
        "comment": "Hello world inside dark",
        "subComments": {
            "commentId": 3,
            "commentedBy": "jaril 3",
            "date": "",
            "comment": "wow working great"
        }
    }
}

答案 2 :(得分:1)

    const objData =   {"comments":{
            "commentedBy" : "jaril1",
            "date" : "",
            "comment" : "Hello world",
            "subComments" : {
                "commentedBy" : "jaril 2",
                "date" : "",
                "comment" : "Hello world inside dark",
                "subComments" :{
                    "commentedBy": "jaril 3",
                    "date": "",
                    "comment": "wow working great"
                }
            }
        }
    }

    let id = 1;
    function addId(obj) {
     obj.commentId = id++;
       if (obj.subComments) {
          addId(obj.subComments);
       }
    }
            
    addId(objData.comments);
    
    console.log(objData);

这将为每个评论添加commentId,无论有多少子评论。

答案 3 :(得分:1)

var objData =   {"comments":{
     "commentedBy" : "jaril1",
     "date" : "",
     "comment" : "Hello world",
     "subComments" : {
         "commentedBy" : "jaril 2",
         "date" : "",
         "comment" : "Hello world inside dark",
         "subComments" :{
             "commentedBy": "jaril 3",
             "date": "",
             "comment": "wow working great"
         }
     }
 } };

 var Id = 1;
 objData.commentId = Id;

 function addCommentId(comments) {
    if (comments.subComments) {
      comments.subComments.commentId = Id++;
      addCommentId(comments.subComments);
    } 
    return comments;
 }

 var data = addCommentId(objData);
 console.log(data);