How can I combine two arrays into one?

  • 0
    // Первый массив
    const defaultTest = [{
        main: "first1",
        references: [
            { something: "something1" },
            { something: "something2" },
        ]
    }]
    
    // Второй массив
    const systemTest = [{
        main: "first2",
        references: [
            { something: "something11" },
            { something: "something22" },
        ]
    }]
    
    // То что я хочу получить на выходе 
    const test = [
        { something: "something1", main: "first1", defaultTest: true },
        { something: "something2", main: "first1", defaultTest: true },
        { something: "something11", main: "first2", systemTest: true },
        { something: "something22", main: "first2", systemTest: true },
    ]
    JavaScript Sofia Morgan, Jan 18, 2020

  • 2 Answers
  • 0
    const test = [
    ...defaultTest.references.map(({ something }) => ({ something, main: defaultTest.main, defaultTest: true })),
    ...systemTest.references.map(({ something }) => ({ something, main: systemTest.main, systemTest: true })),
    ];
    Anonymous

  • 0
    Object
    .entries({ defaultTest, systemTest })
    .map(([ k, v ]) => v.map(({ references, ...n }) => references.map(m => ({
    ...m,
    ...n,
    [k]: true,
    }))))
    .flat(2)
    Anonymous

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!