How can I convert an array of string arrays into an array of objects?

Luan Tran

I'm building myself an admin dashboard in NextJS 13, and I have been struggling with converting arrays using ES6 functions.

For instance, I have the following array:

["","1","Mobile Phones"], //corresponding to ParentID, ID, Name
["1","2","Apple"],
["1","3","Samsung"],
["","4","Tablets"],
["4","5","Huawei"],

How can I convert it to the below object array like the below?

[
  {
    id: 1,
    name: "Mobile Phones",
    children: [
      {
        id: 2,
        name: "Apple",
        children: [],
      },
      {
        id: 3,
        name: "Samsung",
        children: [],
      },
    ],
  },
  {
    id: 4,
    name: "Tablets",
    children: [
      {
        id: 5,
        name: "Huawei",
        children: [],
      },
    ],
  },
];

I have tried using array.map but I got the wrong result as shown below

[
  {
    id: 1,
    name: "Mobile Phones",
    children: [],
  },
  {
    id: 2,
    name: "Apple",
    children: [],
  },

  {
    id: 3,
    name: "Samsung",
    children: [],
  },
  {
    id: 4,
    name: "Tablets",
    children: [],
  },
  {
    id: 5,
    name: "Huawei",
    children: [],
  },
];
Ahmar

Create a map of non empty parentID to object. Then push the top level objects in the result array.

function buildObjectFromArray(arr) 
{
  const map = new Map();

  // Create a map of parent IDs to their respective objects
  for (const [parentId, id, name] of arr) {
    map.set(id, { id: parseInt(id), name, children: [] });
    if (parentId !== "") {
      const parent = map.get(parentId);
      parent.children.push(map.get(id));
    }
  }

  // Find the top-level objects (ParentID === "")
  const result = [];
  for (const [parentId, id, name] of arr) {
    if (parentId === "") {
      result.push(map.get(id));
    }
  }

  return result;
}



const objArr = buildObjectFromArray(
  [
    ["", "1", "Mobile Phones"],
    ["1", "2", "Apple"],
    ["1", "3", "Samsung"],
    ["", "4", "Tablets"],
    ["4", "5", "Huawei"],
    ["", "6", "X"],
    ["6", "7", "Y"],
    ["7", "8", "Z"],
]);

console.log(objArr);

Note: This solution assumes that the parent is declared before its children in the input.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I convert string to array of objects in JavaScript

How can I convert a String to a char array?

How can I convert an Int array into a String? array in Swift

Convert array of objects to array of arrays

How to convert a string with arrays to an array

How can I convert this object of objects into an array of objects?

How can I convert an array to string in swift?

How can I convert plain nested array to collection of entity objects?

How can I convert multi-line string to objects or an array?

How to convert an object with arrays as values to an array of objects

How can I convert an array of arrays to an array of objects, selecting the key from other array?

How can I convert string to a array?

How can I search the array with some of the letter/s of arrays objects?

How can I use Lambda to convert a String array to Integer array?

How to convert ajax array of objects into array of arrays

How can I sort an array of objects in an array by string value?

How to convert an array of objects and arrays of objects to array of objects

how to convert string into array of objects

Convert array of objects to array of arrays?

How can I convert string to array in JavaScript?

How can I convert an array of class objects to a dataframe with columns in Pandas?

How can I convert this list of pairs into an object that contains an array of arrays?

How can I convert a string which contains an Array of Objects back to an Array?

How can i convert json array in string format into array[]

How to convert array of objects to array of arrays in javascript

How to convert array of arrays to array of objects in javascript

How can I convert an array of objects to array with a unique id in javascript?

how to convert string array of objects to array of objects

How can I convert a string containing data in a specific format into an array of associative arrays in PHP?