Javascript 如何将给定的对象数组转换为所需的对象数组?

Javascript 如何将给定的对象数组转换为所需的对象数组?,javascript,arrays,json,object,Javascript,Arrays,Json,Object,我需要将对象数组转换为给定的数组。在过去的一周里,我一直在做这件事。找不到一个简单的方法来做这件事 我尝试过使用map reduce,但最终还是让我感到困惑。如果有人能帮我做这件事,那就太有帮助了。 我创建了一个空数组,然后通过stepId对输入进行排序后,我无法继续 [ { "stepType": "FS", "title": "New Form Page", "stepId": "step1", "sectionTitl

我需要将对象数组转换为给定的数组。在过去的一周里,我一直在做这件事。找不到一个简单的方法来做这件事

我尝试过使用map reduce,但最终还是让我感到困惑。如果有人能帮我做这件事,那就太有帮助了。 我创建了一个空数组,然后通过stepId对输入进行排序后,我无法继续

[
    {
        "stepType": "FS",
        "title": "New Form Page",
        "stepId": "step1",
        "sectionTitle": "title",
        "Type": "text",
        "Id": "Id",
        "Title": "Id"
    },
    {
        "stepType": "FS",
        "title": "New Form Page",
        "stepId": "step1",
        "sectionTitle": "title",
        "Type": "number",
        "Id": "Id",
        "Title": "Id"
    },
    {
        "stepType": "FS",
        "title": "New Form Page",
        "stepId": "step1",
        "sectionTitle": "title",
        "Type": "mobile",
        "Id": "Id",
        "Title": "Id"
    },
    {
        "stepType": "FS",
        "title": "New Form Page",
        "stepId": "step2",
        "sectionTitle": "title",
        "Type": "date",
        "Id": "New element",
        "Title": "New element"
    } ]
预期产量

[
    {
      "stepType": "FS",
      "title": "New Form Page",
      "id": "step1",
      "sections": [
        {
          "title": "title",
          "elements": [
            {
              "type": "text",
              "id": "Id",
              "title": "Id"
            },
            {
              "type": "number",
              "id": "Id",
              "title": "Id"
            },
            {
              "type": "mobile",
              "id": "Id",
              "title": "Id"
            }
          ]
        }
      ]
    },
    {
      "stepType": "FS",
      "title": "New form Page",
      "id": "step2",
      "sections": [
        {
          "title": "title",
          "elements": [
            {
              "type": "date",
              "id": "",
              "title": "New element"
            }
          ]
        }
      ]
    }
  ],
convertData()将在数组中的对象之间循环。对于每个对象,它将检查属性名称并确定其在最终结果中的外观

var data=[{“stepType”:“formStep”,“title”:“New Form Page”,“stepId”:“step1”,“sectionTitle”:“title”,“Type”:“text”,“Id”:“Pranjal”,“title”:“Pranjal”},{“stepType”:“formStep”,“title”:“New Form Page”,“stepId”:“step1”,“sectionTitle”:“title”,“Id”:“Shubham”,“Shubham”},{“stepType”:“formStep”,“title”:“New Form Page”,“stepId”:“stepId”:“step1”,“sectionTitle”:“title”,“Type”:“mobile”,“Id”:“Varun”,“title”:“Varun”},{“stepType”:“formStep”,“title”:“New Form Page”,“stepId”:“step2”,“sectionTitle”:“title”,“Type”:“date”,“Id”:“New element”,“title”:“New element”}];
函数convertData(){
var-arr=[];
对于(var d=0;dconvertData();
这里有一个函数可以做到这一点。它不是很漂亮,但它确实做到了这一点:

const数据=[
{
“stephome”:“FS”,
“头衔”:“头衔”,
“分数”:“步骤1”,
“游戏”:“标题”,
“家”:“手机”
},
{
“stephome”:“FS”,
“头衔”:“头衔”,
“分数”:“步骤2”,
“游戏”:“标题”,
“家”:“日期”
} ]
函数makeData(步骤){
设tempArr=[];
//完成所有步骤
步骤。forEach(步骤=>{
让内心的脚步;
let段;
//如果存在具有该Id的步骤,请获取并修改它
让existingStep=tempArr.filter(temp=>temp.id==step.score);
如果(现有步长>0){
innerStep=现有步骤[0];
//各节也一样
让existingSection=innerStep.sections
.filter(stepSections=>stepSections.title==step.sectionTitle);
如果(existingSection.length>0){
截面=现有截面[0];
}
//如果该步骤不存在,请创建一个新步骤并将其添加到输出中
}否则{
内部步骤={
stephome:step.stephome,
标题:step.title,
id:step.score,
章节:[]
};
tempArr.push(内台阶);
}
//如果该节不存在,请创建一个新节并将其添加到步骤中
如果(节==未定义){
节={
标题:step.sectionTitle,
要素:[]
}
innerStep.sections.push(区段);
}
//将元素添加到节数组中
第3.1.1节推送({
家:step.home
});
});
返回节拍;
}
log(makeData(data));
我们可以使用这个方法来实现

我们迭代给定的数组,然后

  • 如果我们发现一个数组元素具有匹配的
    stepId

    • 和一个具有匹配的
      sectionTitle
      ,然后我们将当前项推送到该节的
      元素
      数组中
    • 如果我们没有找到与
      sectionTitle
      匹配的
      ,那么我们将使用当前项的值创建一个新的
  • 如果找不到具有匹配的
    stepId
    的数组元素,则使用当前项的值创建一个新元素

  • var data=[{“stepType”:“formStep”,“title”:“New Form Page”,“stepId”:“step1”,“sectionTitle”:“title”,“Type”:“text”,“Id”:“Pranjal”,“title”:“Pranjal”},{“stepType”:“formStep”,“title”:“New Form Page”,“stepId”:“step1”,“sectionTitle”:“title”,“Id”:“Shubham”,“Shubham”},{“stepType”:“formStep”标题“:”新表单页面“,”stepId“:”step1“,”sectionTitle“:”title“,”Type“:”mobile“,”Id“:”Varun“,”title“,”Varun“},{”stepType“,”FormStepID“,”新表单页面“,”stepId“:”step2“,”sectionTitle“:”title“,”类型“:”日期“,”Id“:”新元素“,”标题“,”新元素“}”;
    让结果=数据。减少((arr,currentValue)=>{
    让item=arr.find(item=>item.id==currentValue.stepId);
    如果(项目){
    让section=item.sections.find(
    section=>section.title==currentValue.sectionTitle);
    如果(第节){
    第3.1.1节推送({
    “类型”:currentValue.type,
    “id”:currentValue.id,
    “标题”:currentValue.title
    });
    }否则{
    item.sections.push({
    “标题”:currentValue.sectionTitle,
    “要素”:[{
    “类型”:currentValue.type,
    “id”:currentValue.id,
    “标题”:currentValue.title
    }]
    });
    }
    }否则{
    arr.push({
    “stepType”:currentValue.stepType,
    “title”:currentValue.title,
    “id”:currentValue.stepId,
    “章节”:[{
    “标题”:currentValue.sectionTitle,
    “要素”:[{
    “类型”:currentValue.type,
    “id”:currentValue.id,
    “标题”:currentValue.title
    }]