繁体   English   中英

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

[英]Javascript object nested value update with unique key

我想在每个子注释中添加唯一的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"
            }
        }
    }
}

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

如果没有为增量没有额外的要求,你可以因为你的数据结构是递归的递归做到这一点。

您定义一个递归函数,如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"
        }
    }
}

  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。

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

暂无
暂无

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

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