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

使用唯一键更新Javascript对象嵌套值,javascript,Javascript,我想在每个子评论中添加唯一的id,评论可能有100多条,我得到这个对象作为输入,我必须添加自动增量并插入数据库,任何帮助都将不胜感激 我想更新此对象 var objData = {"comments":{ "commentedBy" : "jaril1", "date" : "", "comment" : "Hello world", "subComments" : { "commentedBy" : "

我想在每个子评论中添加唯一的
id
,评论可能有100多条,我得到这个对象作为输入,我必须添加自动增量并插入数据库,任何帮助都将不胜感激

我想更新此
对象

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”}}
变量id=1
函数更新注释(CommonObj){
返回commonobj.subcents?{…commonobj,commentId:id++,subcents:updateComment(commonobj.subcents)}:{…commonobj,commentId:id++}
}
console.log(updateComment(objData.comments))
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”}}
变量id=1
函数更新注释(CommonObj){
返回commonobj.subcents?{…commonobj,commentId:id++,subcents:updateComment(commonobj.subcents)}:{…commonobj,commentId:id++}
}

console.log(updateComment(objData.comments))
如果对增量没有额外要求,您可以使用递归来实现,因为您的数据结构是递归的

您可以定义一个递归函数,如
withId
,它接受一个对象和id,增加id,然后调用
子命令
属性(如果存在):

const objData={“注释”:{
“评论人”:“jaril1”,
“日期”:“日期”,
“评论”:“你好,世界”,
“小组委员会”:{
“评论人”:“jaril 2”,
“日期”:“日期”,
“评论”:“黑暗中的你好世界”,
“小组委员会”:{
“评论人”:“jaril 3”,
“日期”:“日期”,
“评论”:“哇,太棒了”
}
}
}
}
id为的常量=({commentedBy,date,comment,subcomment},id)=>{
if(子命令的类型==='undefined'){
返回{commentId:id+1,commentedBy,date,comment};
}否则{
让incrementedId=id+1;
返回{commentId:incrementedId,commentedBy,date,comment,Subcomment:withId(subComments,incrementedId)};
}
};
const withId=(objectData)=>withId(objectData.comments,0);

console.log(带id(objData))如果没有对增量的额外要求,可以使用递归来实现,因为您的数据结构是递归的

您可以定义一个递归函数,如
withId
,它接受一个对象和id,增加id,然后调用
子命令
属性(如果存在):

const objData={“注释”:{
“评论人”:“jaril1”,
“日期”:“日期”,
“评论”:“你好,世界”,
“小组委员会”:{
“评论人”:“jaril 2”,
“日期”:“日期”,
“评论”:“黑暗中的你好世界”,
“小组委员会”:{
“评论人”:“jaril 3”,
“日期”:“日期”,
“评论”:“哇,太棒了”
}
}
}
}
id为的常量=({commentedBy,date,comment,subcomment},id)=>{
if(子命令的类型==='undefined'){
返回{commentId:id+1,commentedBy,date,comment};
}否则{
让incrementedId=id+1;
返回{commentId:incrementedId,commentedBy,date,comment,Subcomment:withId(subComments,incrementedId)};
}
};
const withId=(objectData)=>withId(objectData.comments,0);
console.log(带id(objData))
const objData={“注释”:{
“评论人”:“jaril1”,
“日期”:“日期”,
“评论”:“你好,世界”,
“小组委员会”:{
“评论人”:“jaril 2”,
“日期”:“日期”,
“评论”:“黑暗中的你好世界”,
“小组委员会”:{
“评论人”:“jaril 3”,
“日期”:“日期”,
“评论”:“哇,太棒了”
}
}
}
}
设id=1;
附加功能(obj){
obj.commentId=id++;
if(对象子元素){
附加(对象子组件);
}
}
添加(objData.comments);
console.log(objData)
const objData={“注释”:{
“评论人”:“jaril1”,
“日期”:“日期”,
“评论”:“你好,世界”,
“小组委员会”:{
“评论人”:“jaril 2”,
“日期”:“日期”,
“评论”:“黑暗中的你好世界”,
“小组委员会”:{
“评论人”:“jaril 3”,
“日期”:“日期”,
“评论”:“哇,太棒了”
}
}
}
}
设id=1;
附加功能(obj){
obj.commentId=id++;
if(对象子元素){
附加(对象子组件);
}
}
添加(objData.comments);

console.log(objData)这是奇怪的格式。“comments”和“subComments”属性应该包含一个数组,对吗?否则,您将始终只有一个注释和子注释…您是否定义了此结构或对其没有任何影响?这是一种奇怪的格式。“comments”和“subComments”属性应该包含一个数组,对吗?否则,您将始终只有一个注释和子注释…您是否定义了此结构或对其没有任何影响?
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);