Home > Net >  How to get the array values using Array Map?
How to get the array values using Array Map?

Time:10-07

How to get only the values from the Array which have pairs using map. I don't want to include the index name instead I want to get the desired outcome below with the same format.

[
   {
      "course_code":"BSEE",
      "total":1
   },
   {
      "course_code":"BSIT",
      "total":3
   },
   {
      "course_code":"BSME",
      "total":0
   }
]

This is my desire outcome

[
   {
      "BSEE",
      1
   },
   {
      "BSIT",
      3
   },
   {
      "BSME",
      0
   },
]

CodePudding user response:

Array.map approach

const data = [
  { course_code: "BSEE", total: 1 },
  { course_code: "BSIT", total: 3 },
  { course_code: "BSME", total: 0 },
  { course_code: "BSOA-LT", total: 0 },
  { course_code: "DICT", total: 0 },
  { course_code: "DOMT", total: 0 },
];
const output = data.map(({ course_code, total }) => [course_code, total]);
console.log(output);

  • Related