【问题标题】:convert json from multiple parent id to single parent in javascript在javascript中将json从多个父ID转换为单父
【发布时间】:2020-08-04 06:38:47
【问题描述】:

我有问题,我想将多个 parentId JSON 转换为单个 parentId JSON
这是我将提供给我的 js 函数的多个父 json 示例。

[{
    "id": "1",
    "name": "first_name",
    "label": "First Name",
    "placeholder": "Enter Your First Name",
    "required": "yes",
    "inputType": "text",
    "options": [],
    "parentId": "",
    "isPost": true
  },
  {
    "id": "4",
    "name": "ready_to_provide_email_address",
    "label": "Ready to Provide Email Address",
    "placeholder": "Enter Your Phone Number",
    "required": "yes",
    "inputType": "radio",
    "options": [{
        "label": "Yes",
        "name": "ready_to_provide_email",
        "value": "Yes",
        "id": "5"
      },
      {
        "label": "No",
        "name": "ready_to_provide_email",
        "value": "No",
        "id": "6"
      }
    ],
    "parentId": "",
    "isPost": false
  },
  {
    "id": "7",
    "name": "email",
    "label": "Email",
    "placeholder": "Enter Your Email",
    "required": "yes",
    "inputType": "text",
    "options": [],
    "parentId": ["5", "6"],
    "isPost": true
  }
]

这是我想要输出的单父 json。

[{
    "id": "1",
    "name": "first_name",
    "label": "First Name",
    "placeholder": "Enter Your First Name",
    "required": "yes",
    "inputType": "text",
    "options": [],
    "parentId": "",
    "isPost": true
  },
  {
    "id": "4",
    "name": "ready_to_provide_email_address",
    "label": "Ready to Provide Email Address",
    "placeholder": "Enter Your Phone Number",
    "required": "yes",
    "inputType": "radio",
    "options": [{
        "label": "Yes",
        "name": "ready_to_provide_email",
        "value": "Yes",
        "id": "5"
      },
      {
        "label": "No",
        "name": "ready_to_provide_email",
        "value": "No",
        "id": "6"
      }
    ],
    "parentId": "",
    "isPost": false
  }, {
    "id": "7",
    "name": "email",
    "label": "Email",
    "placeholder": "Enter Your Email",
    "required": "yes",
    "inputType": "text",
    "options": [],
    "parentId": "5",
    "isPost": true
  },
  {
    "id": "8",
    "name": "email",
    "label": "Email",
    "placeholder": "Enter Your Email",
    "required": "yes",
    "inputType": "text",
    "options": [],
    "parentId": "6",
    "isPost": true
  }
]

注意:在我的例子中,object.option.id 是 element 的父 id。请帮我。非常感谢您。

【问题讨论】:

标签: javascript arrays json reactjs


【解决方案1】:
let result = input.map(a => a.id);
        var max_of_array = Math.max.apply(Math, result);
        var newArr = [];
        for (var i in input) {
            var first = true;
            let objCopy = Object.assign({}, input[i].parentId);
            input[i]=Object.assign(input[i],{"initObjectPos":i});
            if(input[i].parentId.length == 0){
                input[i].parentId="";
            }
            for (var x in objCopy) {
                var y = input[i];

                if (!first) {
                    y = Object.assign({}, y);
                    max_of_array++;
                    y.id = max_of_array.toString();
                    newArr.push(y);
                }
                y.parentId = objCopy[x];
                first = false;
            }
        }
        var output = input.concat(newArr);
        return output;

【讨论】:

    【解决方案2】:

    var input = [{
        "id": "1",
        "name": "first_name",
        "label": "First Name",
        "placeholder": "Enter Your First Name",
        "required": "yes",
        "inputType": "text",
        "options": [],
        "parentId": "",
        "isPost": true
      },
      {
        "id": "4",
        "name": "ready_to_provide_email_address",
        "label": "Ready to Provide Email Address",
        "placeholder": "Enter Your Phone Number",
        "required": "yes",
        "inputType": "radio",
        "options": [{
            "label": "Yes",
            "name": "ready_to_provide_email",
            "value": "Yes",
            "id": "5"
          },
          {
            "label": "No",
            "name": "ready_to_provide_email",
            "value": "No",
            "id": "6"
          }
        ],
        "parentId": "",
        "isPost": false
      },
      {
        "id": "7",
        "name": "email",
        "label": "Email",
        "placeholder": "Enter Your Email",
        "required": "yes",
        "inputType": "text",
        "options": [],
        "parentId": ["5", "6"],
        "isPost": true
      }
    ];
    
    let result = input.map(a => a.id);
    var max_of_array = Math.max.apply(Math, result);
    var newArr = [];
    
    for (var i in input) {
      var first = true;
      let objCopy = Object.assign({}, input[i].parentId);
      for (var x in objCopy) {
        var y = input[i];
        if (!first) {
          y = Object.assign({}, y);
          max_of_array++;
          y.id = max_of_array.toString();
          newArr.push(y);
        }
        y.parentId = objCopy[x];
        first = false;
      }
    }
    
    var output = input.concat(newArr);
    console.log(output);

    【讨论】:

    • 请在此 json jsoneditoronline.org/… 上测试您的功能,这样您将获得 id 10 两次,这是我的问题,所有对象都应该具有唯一的 id 谢谢
    • 我已经更新了我的解决方案,并且在针对提供的新 JSON 运行时看不到任何重复的 id 属性。
    • 是的,当我复制新的代码重复时,请删除您所做的更改,先生,我可以让您 watsapp 或 facebook 解决更多问题,如果您给我,我会很高兴非常感谢您的帮助
    • 我认为我所做的主要更改是确保新的ID 属性是字符串而不是数字。如果可行,请接受解决方案。
    • 要接受答案,请点击投票按钮下方的“复选标记”按钮:meta.stackexchange.com/questions/5234/… 谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-07
    • 1970-01-01
    • 2012-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-19
    相关资源
    最近更新 更多