简体   繁体   English

Javascript 在现有 object 中添加 object

[英]Javascript adding an object inside an existing object

I want the user to have the option to add more stepTypes, stepCodes and properties.我希望用户可以选择添加更多 stepTypes、stepCodes 和属性。 He can add an stepCode with an existing StepType, or with a different stepType, so, the object would like similar to this:他可以使用现有的 StepType 或不同的 stepType 添加 stepCode,因此,object 会与此类似:

目标

You see?你看? In the stepType called 'guide', I have 2 stepCodes (G019, G040).在名为“guide”的 stepType 中,我有 2 个 stepCode(G019、G040)。 In the stepType called 'success', I have just one (S003), and so on.在名为“成功”的 stepType 中,我只有一个 (S003),依此类推。 Since I'm newbie with js and even more with objects, I'd like you guys to help me creating a function that checks if the stepType already exists, and then adds another stepCode to it (with its properties).由于我是 js 的新手,甚至是对象的新手,我希望你们帮助我创建一个 function 来检查 stepType 是否已经存在,然后向它添加另一个 stepCode(及其属性)。 And, if it doesn't exist yet, I want this function to create this new stepType, with the stepCode and its properties.而且,如果它还不存在,我希望这个 function 使用 stepCode 及其属性创建这个新的 stepType。

Currently, my code looks like this:目前,我的代码如下所示:

 const checkStep = () => {
    if (!Object.keys(procedures).length) {
      let proc = 
        {[key]:
          {
            [stepType]: {
              [stepCode]: {
                [language]: stepText,
                timeout,
                nextInstruction,
              }
            }
          }
        }
      setProcedures(proc)
    }
    else{
      Object.entries(procedures).forEach((p, k) =>{
        ...
      })
    }
  }

I call this function everytime the user clicks the "Add another step" button.每次用户单击“添加另一个步骤”按钮时,我都会将此称为 function。 The first part checks if the object already exists, and, if it doesn't, it creates the object with its key and so on (this part is working).第一部分检查 object 是否已经存在,如果不存在,则使用其密钥创建 object 等等(这部分正在工作)。 What I don't know how to do is the ELSE part.我不知道该怎么做的是 ELSE 部分。 I think we have to check if the stepType already exists in the object called procedures, but I don't know how to do it.我想我们必须检查在object调用程序中是否已经存在stepType,但我不知道该怎么做。 I don't know how to put the stepCode and properties inside the existing object(procedures) either.我也不知道如何将 stepCode 和属性放在现有对象(过程)中。 Maybe I create a variable and do like: setProcedures (...procedures, variable).也许我创建了一个变量并喜欢:setProcedures (...procedures, variable)。 I don't want to lose the content I have in the procedure, just to add more content to it in the way I explained you.我不想丢失程序中的内容,只是按照我向您解释的方式添加更多内容。

PS: All the variables (stepType, stepCode, language, stepText, timeout, nextInstruction) are an useState. PS:所有的变量(stepType、stepCode、language、stepText、timeout、nextInstruction)都是一个useState。 When the user writes anything in the input text field, I set the specific variable with the e.target.value.当用户在输入文本字段中写入任何内容时,我使用 e.target.value 设置特定变量。

Thank you.谢谢你。

You can do the loop on the each keys and if it matches then add to existing data or create a new stepType and add.您可以对每个键执行循环,如果匹配,则添加到现有数据或创建新的 stepType 并添加。

var newStepType = "test", stepCode="test1", language ="en", stepText="hello", timeout=9, nextInstruction="new ins";
    Object.keys(procedure.DOF0014).forEach(key => { 
//if newStepType matches insert stepCode. eg: stepType is "guide"
        if(key === newStepType) { 
            procedure.DOF0014[key] = { ...procedure.DOF0014[key], ...{[stepCode]: {[language]: stepText,timeout,nextInstruction}}}
        }else{
            procedure.DOF0014 = {...procedure.DOF0014, ...{[newStepType]:{[stepCode]: {[language]: stepText,timeout,nextInstruction}}}};
        }
    });

Try this.尝试这个。 I didnt tested code.我没有测试代码。 But hope it works.但希望它有效。 I am sharing the idea how to do.我正在分享如何做的想法。

Object.keys(procedure).forEach(codes => {
  Object.keys(procedure[codes]).forEach(key => { 
            if(key === newStepType) { 
                procedure[codes][key] = { ...procedure[codes][key], ...{[stepCode]: {[language]: stepText,timeout,nextInstruction}}}
            }else{
                procedure[codes] = {...procedure[codes], ...{[newStepType]:{[stepCode]: {[language]: stepText,timeout,nextInstruction}}}};
            }
        });    
  })

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

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