Home > OS >  Better way of doing array Flat
Better way of doing array Flat

Time:09-08

So I a trying to take out the objects from within an array to a new array. But I need a more better and efficient way of doing this.

this is how I did it. First I created a new array through filter1, then I flattened it and then I created another array with filter3.

filter1 = myArray.map((a => a.courseEnrolled);
filter2 = filter1.flat(1);
filter3 = filter2.map((a => a.course);

I am looking for better way of achieving 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",
    },
  ];

CodePudding user response:

Use a flatMap() with a nested map() to get all the name's

Then get all the unique values, and use map() to convert to the final object

myArray = [{name: "john", courseEnrolled: [{course: {name: "react", }, }, {course: {name: "java", }, }, ], }, {name: "Doe", courseEnrolled: [{course: {name: "java", }, }, {course: {name: "angular", }, }, ], }, ];
    
const all = myArray.flatMap(e => e.courseEnrolled.map(c => c.course.name));
const res =  all.filter((v, i, s) => s.indexOf(v) === i).map(name => ({ name }));
console.log(res)

[
  {
    "name": "react"
  },
  {
    "name": "java"
  },
  {
    "name": "angular"
  }
]

CodePudding user response:

Use this :

expectedArray = myArray.map(a => a.courseEnrolled.map((c) => c.course)).flat(4)
uniqueArray = [...new Set(expectedArray.map(c => c.name))].map((c)=>{  return { "name" : c} })

I hope this answer helps you! Comment if you have any questions or doubts.

CodePudding user response:

Lodash, if you don't mind

const  myArray = [{name: "john",courseEnrolled: [{course: {name: "react"}},{course: {name: "java"}}]},{name: "Doe",courseEnrolled: [{course: {name: "java"}},{course: {name: "angular"}}]}];

const result = _(myArray)
  .flatMap(({ courseEnrolled }) => courseEnrolled.map(({ course }) => course))
  .uniqBy('name');
  
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0 }
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lodash.min.js"></script>

CodePudding user response:

Another solution. Kind of one-liner

const myArray = [{name: "john",courseEnrolled: [{course: {name: "react"}},{course: {name: "java"}}]},{name: "Doe",courseEnrolled: [{course: {name: "java"}},{course: {name: "angular"}}]}];

const result = Object.values(myArray.reduce((acc, { courseEnrolled: arr }) => (
  arr.forEach(({ course })=> (acc[course.name] ??= course)), acc
), {}));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0 }

  • Related