簡體   English   中英

我如何比較兩個多維數組並合並為單個數組

[英]how can i compare two multidimensional array and merge into single array

我正在尋找一種基於課程值比較和組合兩個多維對象數組的方法。 我可以使用頂層的過濾方法使其工作,但不確定如何確保所有級別與技能都深度合並。 下面是一個例子:

 var array1 = [ { "course": "Javascript and protractor", "description": "this is test course", "levelwithskills": [ { "name": "Beginner", "skills": [ { "duration": 45, "name": "HTML/CSS/JS", "price": 75 }, { "duration": 45, "name": "Data And Flows", "price": 75 }, { "duration": 45, "name": "Functions And Objects", "price": 75 }, { "duration": 45, "name": "Protractor Project", "price": 75 } ] }, { "name": "Improver", "skills": [ { "duration": 45, "name": "Jasmine", "price": 90 }, { "duration": 45, "name": "Protractor Methods", "price": 90 }, { "duration": 45, "name": "Non Angular App Testing", "price": 90 }, { "duration": 45, "name": "Reports", "price": 90 } ] }, { "name": "Intermediate", "skills": [ { "duration": 45, "name": "Crossbrowser Config", "price": 100 }, { "duration": 45, "name": "Data-Driven", "price": 100 }, { "duration": 45, "name": "Gulp-Integration", "price": 100 }, { "duration": 45, "name": "User Stories To Test", "price": 100 } ] } ], "s3key": "javascsipt.png" }, { "course": "Java and BDD", "id": "564e0758-803f-11e9-ba30-01cdbe15a808", "levelwithskills": [ { "name": "Beginner", "skills": [ { "duration": 45, "name": "HTML/CSS/JS", "price": 1 }, { "duration": 45, "name": "Data and flows", "price": 1 }, { "duration": 45, "name": "OOP", "price": 1 }, { "duration": 45, "name": "Libraries", "price": 1 } ] }, { "name": "Improver", "skills": [ { "duration": 45, "name": "Selenium Webdriver", "price": 90 }, { "duration": 45, "name": "Junit", "price": 90 }, { "duration": 45, "name": "Saucelabs", "price": 90 }, { "duration": 45, "name": "User Stories To Test", "price": 90 } ] }, { "name": "Intermediate", "skills": [ { "duration": 45, "name": "Most Used Methods", "price": 100 }, { "duration": 45, "name": "Git", "price": 100 }, { "duration": 45, "name": "Gherkin", "price": 100 }, { "duration": 45, "name": "Cucumber", "price": 100 } ] }, { "name": "Semi-Advance", "skills": [ { "duration": 45, "name": "Maven", "price": 110 }, { "duration": 45, "name": "Cucumber Data-driven", "price": 11000 }, { "duration": 45, "name": "Jenkins", "price": 110 }, { "duration": 45, "name": "Pipeline", "price": 110 } ] }, { "name": "Advance", "skills": [ { "duration": 45, "name": "Page Object Model", "price": 120 }, { "duration": 45, "name": "Advanced Methods", "price": 120 }, { "duration": 45, "name": "Advanced Logic", "price": 120 }, { "duration": 45, "name": "Your Own Framework", "price": 120 } ] }, { "name": "Professional", "skills": [ { "duration": 45, "name": "Serenit Framework p1", "price": 130 }, { "duration": 45, "name": "Serenit Framework p2", "price": 130 }, { "duration": 45, "name": "Serenit Framework p3", "price": 130 }, { "duration": 45, "name": "Serenit Framework p4", "price": 130 } ] } ], "passion": "Tester", "s3key": "java.png", } ]; var array2 = [ { "course": "Javascript and protractor", "levelwithskills": [ { "name": "Beginner", "skills": [ { "name": "HTML/CSS/JS" } ] } ] } , { "course": "Java and BDD", "levelwithskills": [ { "name": "Beginner", "skills": [ { "name": "HTML/CSS/JS" } ] } ] } ]; const sameData = array1.map( obj => array2.map(obj2 => { if (obj.course === obj2.course) { return { ...obj, levelwithskills: obj.levelwithskills.map( xlvl => obj2.levelwithskills.map(ylvl => { if (xlvl.name === ylvl.name) { return { ...xlvl, skills: xlvl.skills.map( xskill => ylvl.skills.map(yskill => { if (xskill.name === yskill.name) { return { ...xskill, selected: true }; } return xskill; })[0] ) }; } return xlvl; })[0] ) }; } })[0] ); console.log(sameData);

我正在尋找類似的東西並將其合並:

[
  {
  "course": "Javascript and protractor",
  "description": "this is test course",
  "levelwithskills": [
    {
      "name": "Beginner",
      "skills": [
        {
          "duration": 45,
          "name": "HTML/CSS/JS",
          "price": 75,
          "selected": true
        },
        {
          "duration": 45,
          "name": "Data And Flows",
          "price": 75
        },
        {
          "duration": 45,
          "name": "Functions And Objects",
          "price": 75
        },
        {
          "duration": 45,
          "name": "Protractor Project",
          "price": 75
        }
      ]
    },
    {
      "name": "Improver",
      "skills": [
        {
          "duration": 45,
          "name": "Jasmine",
          "price": 90
        },
        {
          "duration": 45,
          "name": "Protractor Methods",
          "price": 90
        },
        {
          "duration": 45,
          "name": "Non Angular App Testing",
          "price": 90
        },
        {
          "duration": 45,
          "name": "Reports",
          "price": 90
        }
      ]
    },
    {
      "name": "Intermediate",
      "skills": [
        {
          "duration": 45,
          "name": "Crossbrowser Config",
          "price": 100
        },
        {
          "duration": 45,
          "name": "Data-Driven",
          "price": 100
        },
        {
          "duration": 45,
          "name": "Gulp-Integration",
          "price": 100
        },
        {
          "duration": 45,
          "name": "User Stories To Test",
          "price": 100
        }
      ]
    }
  ],
  "s3key": "javascsipt.png"
},
{
  "course": "Java and BDD",
  "id": "564e0758-803f-11e9-ba30-01cdbe15a808",
  "levelwithskills": [
    {
      "name": "Beginner",
      "skills": [
        {
          "duration": 45,
          "name": "HTML/CSS/JS",
          "price": 1
          "selected": true
        },
        {
          "duration": 45,
          "name": "Data and flows",
          "price": 1
        },
        {
          "duration": 45,
          "name": "OOP",
          "price": 1
        },
        {
          "duration": 45,
          "name": "Libraries",
          "price": 1
        }
      ]
    },
    {
      "name": "Improver",
      "skills": [
        {
          "duration": 45,
          "name": "Selenium Webdriver",
          "price": 90
        },
        {
          "duration": 45,
          "name": "Junit",
          "price": 90
        },
        {
          "duration": 45,
          "name": "Saucelabs",
          "price": 90
        },
        {
          "duration": 45,
          "name": "User Stories To Test",
          "price": 90
        }
      ]
    },
    {
      "name": "Intermediate",
      "skills": [
        {
          "duration": 45,
          "name": "Most Used Methods",
          "price": 100
        },
        {
          "duration": 45,
          "name": "Git",
          "price": 100
        },
        {
          "duration": 45,
          "name": "Gherkin",
          "price": 100
        },
        {
          "duration": 45,
          "name": "Cucumber",
          "price": 100
        }
      ]
    },
    {
      "name": "Semi-Advance",
      "skills": [
        {
          "duration": 45,
          "name": "Maven",
          "price": 110
        },
        {
          "duration": 45,
          "name": "Cucumber Data-driven",
          "price": 11000
        },
        {
          "duration": 45,
          "name": "Jenkins",
          "price": 110
        },
        {
          "duration": 45,
          "name": "Pipeline",
          "price": 110
        }
      ]
    },
    {
      "name": "Advance",
      "skills": [
        {
          "duration": 45,
          "name": "Page Object Model",
          "price": 120
        },
        {
          "duration": 45,
          "name": "Advanced Methods",
          "price": 120
        },
        {
          "duration": 45,
          "name": "Advanced Logic",
          "price": 120
        },
        {
          "duration": 45,
          "name": "Your Own Framework",
          "price": 120
        }
      ]
    },
    {
      "name": "Professional",
      "skills": [
        {
          "duration": 45,
          "name": "Serenit Framework p1",
          "price": 130
        },
        {
          "duration": 45,
          "name": "Serenit Framework p2",
          "price": 130
        },
        {
          "duration": 45,
          "name": "Serenit Framework p3",
          "price": 130
        },
        {
          "duration": 45,
          "name": "Serenit Framework p4",
          "price": 130
        }
      ]
    }
  ],
  "passion": "Tester",
  "s3key": "java.png",
}
]

深度合並對象/數組:

取自 drupal - drupal_array_merge_deep_array並用 JS 重寫)

function array_merge_deep_array(arrays) {
  let result = [];
  for (let i in arrays){
    for (let key in arrays[i]){
      const value = arrays[i][key];
      // Renumber integer keys
      if (typeof(key) === 'number') {
        result.push(value);
      }
      else if (result[key] && typeof(result[key]) === 'object' && typeof(value) === 'object') {
        result[key] = array_merge_deep_array([
          result[key],
          value,
        ]);
      }
      else {
        result[key] = value;
      }
    }
  }
  return result;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM