简体   繁体   English

在JavaScript中将对象添加到复杂的json

[英]add object to a complex json in javascript

Suppose there are two json objects as 假设有两个json对象为

1. 1。

{
  "conditionTemp": null,
  "value": null,
  "variableValue": "flowParameters_3"
}

or 要么

{
  "conditionTemp": {
    "functionID": "func_1",
    "parameters": [{}]
  },
  "value": null,
  "variableValue": null
}

and

2. 2。

{
  "conditionTemp": {
    "functionID": "func_1",
    "parameters": [{
        "conditionTemp": null,
        "value": null,
        "variableValue": "flowParameters_3"
      },
      {
        "conditionTemp": {
          "functionID": "func_1",
          "parameters": [{}]
        },
        "value": null,
        "variableValue": "null"
      },
      {}
    ]
  },
  "value": null,
  "variableValue": null
}

ie the second object will have ("conditionTemp", "value", "variable"), 即第二个对象将具有(“ conditionTemp”,“ value”,“ variable”),

the first "conditionTemp" will have "functionID", "parameters" 第一个“ conditionTemp”将具有“ functionID”,“ parameters”

inside "parameters" we can have any no. 在“参数”中,我们可以没有任何编号。 of objects. 对象。 If inside parameters, the the object's "conditionTemp" value is not null , we have to check the parameter object inside of that. 如果在参数内部,则对象的“ conditionTemp”值不为null ,我们必须检查其中的参数对象。 If the parameter object is empty, we have to insert the **first object there.** 如果参数对象为空,则必须在其中插入“第一个对象”

So for the above jsons, on adding the first object onto the second, the resultant json will be 因此,对于上述json,在将第一个对象添加到第二个对象后,生成的json将是

{
  "conditionTemp": {
    "functionID": "func_1",
    "parameters": [{
        "conditionTemp": null,
        "value": null,
        "variableValue": "flowParameters_3"
      },
      {
        "conditionTemp": {
          "functionID": "func_1",
          "parameters": [{
                 "conditionTemp": null,
                 "value": null,
                 "variableValue": "flowParameters_3"
           }]
        },
        "value": null,
        "variableValue": "null"
      },
      {}
    ]
  },
  "value": null,
  "variableValue": null
}

First level would be like this: 第一层是这样的:

 var obj_a = { "conditionTemp": { "functionID": "func_1", "parameters": [{ "conditionTemp": null, "value": null, "variableValue": "flowParameters_3" }, { "conditionTemp": { "functionID": "func_1", "parameters": [{}] }, "value": null, "variableValue": "null" }, {} ] }, "value": null, "variableValue": null }; var obj_b = { "conditionTemp": null, "value": null, "variableValue": "flowParameters_3" }; var final_obj = Object.keys(obj_a).reduce(function(data, key) { if (obj_a[key] == null && obj_b[key] != null) data[key] = obj_b[key]; else data[key] = obj_a[key]; return data; }, {}); console.log(final_obj); 

Second and further levels would be tricky. 第二和更高的级别将是棘手的。 Is the format always like that? 格式总是这样吗? In your example, values could be (string, null, array of objects)... are there other formats not mentioned or that you wouldn't know? 在您的示例中,值可以是(字符串,空值,对象数组)...是否有其他未提及的格式,或者您不知道?

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

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