Attempting to extract objects from an array to a new array. Searching for a more efficient method.
Approach used: Created a new array with filter1, flattened it, then created another array with filter3.
filter1 = myArray.map((a => a.courseEnrolled);
filter2 = filter1.flat(1);
filter3 = filter2.map((a => a.course);
In search of a better solution to achieve the same result.
myArray = [
{
name: "john",
courseEnrolled: [
{
course: {
name: "react",
},
},
{
course: {
name: "java",
},
},
],
},
{
name: "Doe",
courseEnrolled: [
{
course: {
name: "java",
},
},
{
course: {
name: "angular",
},
},
],
},
];
expectedArray = [
{
name: "react",
},
{
name: "java",
},
{
name: "java",
},
{
name: "react",
},
];
uniqueExpectedArray = [
{
name: "react",
},
{
name: "java",
},
{
name: "react",
},
];