Skip to content
Advertisement

Conver an array of array of objects to array of objects

Summary: I have the below list which is Array of Array of Objects. I have tired the below code and I need help with the code.

const sub = [ [ { id: 'ckndqfwk3538691qzbzty14yit', name: 'Middle SubCategory' } ], 
  [ { id: 'cknc642pi436141ptdfzwn3r6d', name: 'New Youth' } ], 
  [ { id: 'ckndqk1lp558551qzbvgbpyjpf', name: 'College Sub' } ], 
  [ { id: 'ckndqtbds606201qzbyedngk56', name: 'One New Sub' } ], 
  [ { id: 'ckndr18n3662731qzbep6m16dq', name: 'Sub Strange' }, 
    { id: 'ckndr5413665081qzbaxjs4gox', name: 'Sub NEw Strange' } ], 
  [ { id: 'ckmpdxg2513831p01say3e3wl', name: 'SB New One' } ] ] 

how can I convert it into something like the below one. Simply an array of objects. Might be simple but I couldn’t get it to work.

const sub = [
  { id: "ckndqfwk3538691qzbzty14yit", name: "Middle SubCategory" },
  { id: "cknc642pi436141ptdfzwn3r6d", name: "New Youth" },
  { id: "ckndqk1lp558551qzbvgbpyjpf", name: "College Sub" },
  { id: "ckndqtbds606201qzbyedngk56", name: "One New Sub" },
  { id: "ckndr18n3662731qzbep6m16dq", name: "Sub Strange" },
  { id: "ckndr5413665081qzbaxjs4gox", name: "Sub NEw Strange" },
  { id: "ckmpdxg2513831p01say3e3wl", name: "SB New One" },
];

here is my code

const categoriesWithSubCategories = quizQuestionCategories.filter(
  (category) => category.subcategories.length > 0
);
const subOnly = categoriesWithSubCategories.map((category) => {
  let subCategories = [];
  category.subcategories.forEach((subcategory) =>
    subCategories.push(subcategory)
  );
  return subCategories;
});

here is the original list in case you want to reproduce

const quizQuestionCategories = [
  {
    id: "ckm4gpm7j942811p1l2kcyru73",
    parentCategory: null,
    name: "Lower Elementary",
    default: false,
    deleted: false,
    subcategories: [],
  },
  {
    id: "ckm4gsy1i952901p1lsmjb73q6",
    parentCategory: null,
    name: "Middle School",
    default: false,
    deleted: false,
    subcategories: [
      {
        id: "ckndqfwk3538691qzbzty14yit",
        name: "Middle SubCategory",
      },
    ],
  },
  {
    id: "ckm4gwciv982031p1l2oduzear",
    parentCategory: null,
    name: "Youth",
    default: false,
    deleted: false,
    subcategories: [
      {
        id: "cknc642pi436141ptdfzwn3r6d",
        name: "New Youth",
      },
    ],
  },
  {
    id: "ckm4h5kuf992741p1l6otps3bd",
    parentCategory: null,
    name: "College",
    default: false,
    deleted: false,
    subcategories: [
      {
        id: "ckndqk1lp558551qzbvgbpyjpf",
        name: "College Sub",
      },
    ],
  },
  {
    id: "ckm4h7uhb1012471p1l37dk4mdp",
    parentCategory: null,
    name: "One More New",
    default: false,
    deleted: false,
    subcategories: [
      {
        id: "ckndqtbds606201qzbyedngk56",
        name: "One New Sub",
      },
    ],
  },
  {
    id: "ckm4hd4vp1032261p1lpfd420an",
    parentCategory: null,
    name: "Strange One",
    default: false,
    deleted: false,
    subcategories: [
      {
        id: "ckndr18n3662731qzbep6m16dq",
        name: "Sub Strange",
      },
      {
        id: "ckndr5413665081qzbaxjs4gox",
        name: "Sub NEw Strange",
      },
    ],
  },
  {
    id: "ckmdz9lpg56301qx2ypz8blon",
    parentCategory: null,
    name: "dddddddd",
    default: false,
    deleted: false,
    subcategories: [],
  },
  {
    id: "ckmdya8um3149641pvhe5ihqyve",
    parentCategory: null,
    name: "WOWOW",
    default: true,
    deleted: false,
    subcategories: [],
  },
  {
    id: "ckmefmz1p81531qx23099v0bt",
    parentCategory: {
      id: "ckluguhvv2639351ptkir2lhfs7",
      name: "High School New",
    },
    name: "Hard Question",
    default: false,
    deleted: false,
    subcategories: [],
  },
  {
    id: "ckmpdjgpq295311p2x0wt70avc",
    parentCategory: null,
    name: "Parent Category New One",
    default: false,
    deleted: false,
    subcategories: [
      {
        id: "ckmpdxg2513831p01say3e3wl",
        name: "SB New One",
      },
    ],
  },
  {
    id: "cknc642pi436141ptdfzwn3r6d",
    parentCategory: {
      id: "ckm4gwciv982031p1l2oduzear",
      name: "Youth",
    },
    name: "New Youth",
    default: false,
    deleted: false,
    subcategories: [],
  },
  {
    id: "ckmpdxg2513831p01say3e3wl",
    parentCategory: {
      id: "ckmpdjgpq295311p2x0wt70avc",
      name: "Parent Category New One",
    },
    name: "SB New One",
    default: false,
    deleted: false,
    subcategories: [],
  },
  {
    id: "ckndqfwk3538691qzbzty14yit",
    parentCategory: {
      id: "ckm4gsy1i952901p1lsmjb73q6",
      name: "Middle School",
    },
    name: "Middle SubCategory",
    default: false,
    deleted: false,
    subcategories: [],
  },
  {
    id: "ckndqk1lp558551qzbvgbpyjpf",
    parentCategory: {
      id: "ckm4h5kuf992741p1l6otps3bd",
      name: "College",
    },
    name: "College Sub",
    default: false,
    deleted: false,
    subcategories: [],
  },
  {
    id: "ckndqtbds606201qzbyedngk56",
    parentCategory: {
      id: "ckm4h7uhb1012471p1l37dk4mdp",
      name: "One More New",
    },
    name: "One New Sub",
    default: false,
    deleted: false,
    subcategories: [],
  },
  {
    id: "ckndr18n3662731qzbep6m16dq",
    parentCategory: {
      id: "ckm4hd4vp1032261p1lpfd420an",
      name: "Strange One",
    },
    name: "Sub Strange",
    default: false,
    deleted: false,
    subcategories: [],
  },
  {
    id: "ckndr5413665081qzbaxjs4gox",
    parentCategory: {
      id: "ckm4hd4vp1032261p1lpfd420an",
      name: "Strange One",
    },
    name: "Sub NEw Strange",
    default: false,
    deleted: false,
    subcategories: [],
  },
];

Advertisement

Answer

You can do this in one step using flatMap

quizQuestionCategories.flatMap(q => q.subcategories)

const quizQuestionCategories = [
    {
        id: "ckm4gpm7j942811p1l2kcyru73",
        parentCategory: null,
        name: "Lower Elementary",
        default: false,
        deleted: false,
        subcategories: [],
    },
    {
        id: "ckm4gsy1i952901p1lsmjb73q6",
        parentCategory: null,
        name: "Middle School",
        default: false,
        deleted: false,
        subcategories: [
            {
                id: "ckndqfwk3538691qzbzty14yit",
                name: "Middle SubCategory",
            },
        ],
    },
    {
        id: "ckm4gwciv982031p1l2oduzear",
        parentCategory: null,
        name: "Youth",
        default: false,
        deleted: false,
        subcategories: [
            {
                id: "cknc642pi436141ptdfzwn3r6d",
                name: "New Youth",
            },
        ],
    },
    {
        id: "ckm4h5kuf992741p1l6otps3bd",
        parentCategory: null,
        name: "College",
        default: false,
        deleted: false,
        subcategories: [
            {
                id: "ckndqk1lp558551qzbvgbpyjpf",
                name: "College Sub",
            },
        ],
    },
    {
        id: "ckm4h7uhb1012471p1l37dk4mdp",
        parentCategory: null,
        name: "One More New",
        default: false,
        deleted: false,
        subcategories: [
            {
                id: "ckndqtbds606201qzbyedngk56",
                name: "One New Sub",
            },
        ],
    },
    {
        id: "ckm4hd4vp1032261p1lpfd420an",
        parentCategory: null,
        name: "Strange One",
        default: false,
        deleted: false,
        subcategories: [
            {
                id: "ckndr18n3662731qzbep6m16dq",
                name: "Sub Strange",
            },
            {
                id: "ckndr5413665081qzbaxjs4gox",
                name: "Sub NEw Strange",
            },
        ],
    },
    {
        id: "ckmdz9lpg56301qx2ypz8blon",
        parentCategory: null,
        name: "dddddddd",
        default: false,
        deleted: false,
        subcategories: [],
    },
    {
        id: "ckmdya8um3149641pvhe5ihqyve",
        parentCategory: null,
        name: "WOWOW",
        default: true,
        deleted: false,
        subcategories: [],
    },
    {
        id: "ckmefmz1p81531qx23099v0bt",
        parentCategory: {
            id: "ckluguhvv2639351ptkir2lhfs7",
            name: "High School New",
        },
        name: "Hard Question",
        default: false,
        deleted: false,
        subcategories: [],
    },
    {
        id: "ckmpdjgpq295311p2x0wt70avc",
        parentCategory: null,
        name: "Parent Category New One",
        default: false,
        deleted: false,
        subcategories: [
            {
                id: "ckmpdxg2513831p01say3e3wl",
                name: "SB New One",
            },
        ],
    },
    {
        id: "cknc642pi436141ptdfzwn3r6d",
        parentCategory: {
            id: "ckm4gwciv982031p1l2oduzear",
            name: "Youth",
        },
        name: "New Youth",
        default: false,
        deleted: false,
        subcategories: [],
    },
    {
        id: "ckmpdxg2513831p01say3e3wl",
        parentCategory: {
            id: "ckmpdjgpq295311p2x0wt70avc",
            name: "Parent Category New One",
        },
        name: "SB New One",
        default: false,
        deleted: false,
        subcategories: [],
    },
    {
        id: "ckndqfwk3538691qzbzty14yit",
        parentCategory: {
            id: "ckm4gsy1i952901p1lsmjb73q6",
            name: "Middle School",
        },
        name: "Middle SubCategory",
        default: false,
        deleted: false,
        subcategories: [],
    },
    {
        id: "ckndqk1lp558551qzbvgbpyjpf",
        parentCategory: {
            id: "ckm4h5kuf992741p1l6otps3bd",
            name: "College",
        },
        name: "College Sub",
        default: false,
        deleted: false,
        subcategories: [],
    },
    {
        id: "ckndqtbds606201qzbyedngk56",
        parentCategory: {
            id: "ckm4h7uhb1012471p1l37dk4mdp",
            name: "One More New",
        },
        name: "One New Sub",
        default: false,
        deleted: false,
        subcategories: [],
    },
    {
        id: "ckndr18n3662731qzbep6m16dq",
        parentCategory: {
            id: "ckm4hd4vp1032261p1lpfd420an",
            name: "Strange One",
        },
        name: "Sub Strange",
        default: false,
        deleted: false,
        subcategories: [],
    },
    {
        id: "ckndr5413665081qzbaxjs4gox",
        parentCategory: {
            id: "ckm4hd4vp1032261p1lpfd420an",
            name: "Strange One",
        },
        name: "Sub NEw Strange",
        default: false,
        deleted: false,
        subcategories: [],
    },
];

console.log(quizQuestionCategories.flatMap(q => q.subcategories));

Reduce is also an option if you’re working somewhere where flat and flatMap are not supported:

quizQuestionCategories.reduce((acc, q) => acc.concat(q.subcategories), [])
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement