Attempting to merge an array of objects with array while keeping the order of the original array and adding any new values onto the end. Can this be accomplished using an ES6/ES7 syntax one-liner?
const arr1 = [7, 1, 2, 5, 8];
const arr2 = [
{id: 8, type: "MultiChoiceMultiAnswer", name: "Another Test Property"},
{id: 5, type: "MultiChoiceMultiAnswer", name: "test"},
{id: 2, type: "Text", name: "Test Create Property"},
{id: 10, type: "Label", name: "New Label"},
{id: 1, type: "Text", name: "Test Text Property"},
{id: 7, type: "Label", name: "This is a crazy new one"},
{id: 9, type: "Text", name: "New Text"}
];
I have attempted the following which results in all the keys being added into an array with the desired order with the second array added as a new key on the end:
const arr3 = [];
arr3 = arr1.map(x => ([...arr1, arr2]));
Output:
arr3 = [
[7, 1, 2, 5, 8,
[
{id: 8, type: "MultiChoiceMultiAnswer", name: "Another Test Property"},
{id: 5, type: "MultiChoiceMultiAnswer", name: "test"},
{id: 2, type: "Text", name: "Test Create Property"},
{id: 10, type: "Label", name: "New Label"},
{id: 1, type: "Text", name: "Test Text Property"},
{id: 7, type: "Label", name: "This is a crazy new one"},
{id: 9, type: "Text", name: "New Text"}
]
]
];
I have also attempted with which results in an array of falses:
const arr3 = [];
arr3 = arr1.map(id => arr3.push(arr2.id === id));
Output:
arr3 = [ false, false, false, false, false ];
The expected output is a merged, single array of objects:
const arr3 = [
{id: 7, type: "Label", name: "This is a crazy new one"},
{id: 1, type: "Text", name: "Test Text Property"},
{id: 2, type: "Text", name: "Test Create Property"},
{id: 5, type: "MultiChoiceMultiAnswer", name: "test"},
{id: 8, type: "MultiChoiceMultiAnswer", name: "Another Test Property"},
{id: 10, type: "Label", name: "New Label"},
{id: 9, type: "Text", name: "New Text"}
];