简体   繁体   English

如何在 Javascript json 变量中添加新键及其值

[英]how can add new key and it's value in Javascript json variable

Suppose that I have a variable user and I need to append a new question as a key and its value to that newly created id假设我有一个可变用户,我需要 append 一个新问题作为键,它的值是新创建的 id

users variable用户变量

const  users= {
      johndoe: {
        answers:{
            "6ni6ok3ym7mf1p33lnez": "optionTwo",
            "vthrdm985a262al8qx3do": "optionTwo",
            "xj352vofupe1dqz9emx13r": "optionOne"
        }
       
      },
     joseph: {
        answers:{
            "6ni6ok3ym7mf1p33lnez": "optionTwo",
            
        }
       
      }
      
} 

New key and its value and the user who answered the question新密钥及其值以及回答问题的用户

const questionId = "thisisnewkey";
const newQuestionAnswer = "optionTwo";
const userWhoAnsweredTheQuestion = "joseph";

Before appending a question and its answer its mandatory to find the user who answered that question Expected output在附加问题及其答案之前,必须找到回答该问题的用户预期 output

const  users= {
      johndoe: {
        answers:{
            "6ni6ok3ym7mf1p33lnez": "optionTwo",
            "vthrdm985a262al8qx3do": "optionTwo",
            "xj352vofupe1dqz9emx13r": "optionOne"
        }

      },
     joseph: {
        answers:{
            "6ni6ok3ym7mf1p33lnez": "optionTwo",
             "thisisnewkey":"optionTwo"


        }

      }

} 

You can use bracket notation ( [] ) for dynamic property of the object:您可以对 object 的动态属性使用方括号 ( [] ):

users[userWhoAnsweredTheQuestion].answers[questionId] = newQuestionAnswer;

 const users= { johndoe: { answers:{ "6ni6ok3ym7mf1p33lnez": "optionTwo", "vthrdm985a262al8qx3do": "optionTwo", "xj352vofupe1dqz9emx13r": "optionOne" } }, joseph: { answers:{ "6ni6ok3ym7mf1p33lnez": "optionTwo", } } } const questionId = "thisisnewkey"; const newQuestionAnswer = "optionTwo"; const userWhoAnsweredTheQuestion = "joseph"; users[userWhoAnsweredTheQuestion].answers[questionId] = newQuestionAnswer; console.log(users);

Suppose that I have a variable user and I need to append a new question as a key and its value to that newly created id假设我有一个可变用户,我需要 append 一个新问题作为键及其对新创建的 id 的值

users variable用户变量

const  users= {
      johndoe: {
        answers:{
            "6ni6ok3ym7mf1p33lnez": "optionTwo",
            "vthrdm985a262al8qx3do": "optionTwo",
            "xj352vofupe1dqz9emx13r": "optionOne"
        }
       
      },
     joseph: {
        answers:{
            "6ni6ok3ym7mf1p33lnez": "optionTwo",
            
        }
       
      }
      
} 

New key and its value and the user who answered the question新键及其值以及回答问题的用户

const questionId = "thisisnewkey";
const newQuestionAnswer = "optionTwo";
const userWhoAnsweredTheQuestion = "joseph";

Before appending a question and its answer its mandatory to find the user who answered that question Expected output在附加问题及其答案之前,必须找到回答该问题的用户预期 output

const  users= {
      johndoe: {
        answers:{
            "6ni6ok3ym7mf1p33lnez": "optionTwo",
            "vthrdm985a262al8qx3do": "optionTwo",
            "xj352vofupe1dqz9emx13r": "optionOne"
        }

      },
     joseph: {
        answers:{
            "6ni6ok3ym7mf1p33lnez": "optionTwo",
             "thisisnewkey":"optionTwo"


        }

      }

} 

By using ES6 code should be通过使用 ES6 代码应该是

 ...users,
       [userWhoAnsweredTheQuestion]:{
          ...users[userWhoAnsweredTheQuestion],
              answers: {
              ...users[userWhoAnsweredTheQuestion].answers,
                [questionId]: answer
                    }
                }

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

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