0

I am using react with typescript I have some existing JSON data of employees with respect to their department as a key.

Here is my JSON data sample:

[
  { "department": 1,  "name": "Test", "age": 32, "contact": 242222120, "id": 1 },
  { "department": 1,  "name": "Test", "age": 18, "contact": 242222120, "id": 2 },
  { "department": 1,  "name": "Test", "age": 22, "contact": 242222120, "id": 3 },
  { "department": 2,  "name": "Test", "age": 30, "contact": 242222120, "id": 1 },
  { "department": 2,  "name": "Test", "age": 27, "contact": 242222120, "id": 2 },
  { "department": 3,  "name": "Test", "age": 25, "contact": 242222120, "id": 1 }

]

Now I want to convert it into the department as a key and its respected data nested object like:

like below

{
 1:{[
    { "name": "Test", "age": 32, "contact": 242222120, "id": 1 },
    { "name": "Test", "age": 18, "contact": 242222120, "id": 2 },
    { "name": "Test", "age": 22, "contact": 242222120, "id": 3 }
   ]},
2:{[
    { "name": "Test", "age": 30, "contact": 242222120, "id": 1 },
    { "name": "Test", "age": 27, "contact": 242222120, "id": 2 }
   ]}

}

To do like above I created a type:

type DepartmentType = {
    department: number,
    name: string,
    age: number,
    contact: number,
    id: number
  }

Below I created a key-value dictionary and initialize it as empty

let department: {[department: number]: DepartmentType} = {};
const convertJSON = () => {
    let diclength: number = Object.keys(department).length;
  rawJSONData.forEach((x: DepartmentType) => {
    if(diclength === x.department){
        department[diclength] = {
            name: x.name,
            age: x.age,
            contact: x.contact,
            id: x.id
        }
    }
  })
}

I am not getting how to match department and put same department value in that array

  • Note: that is not JSON. It’s simply an array of objects. JSON is the string representation counterpart. – Terry Feb 05 '22 at 09:49
  • Does this answer your question? [How can I group an array of objects by key?](https://stackoverflow.com/questions/40774697/how-can-i-group-an-array-of-objects-by-key) – Terry Feb 05 '22 at 09:52

2 Answers2

0

you could use a reduce like that :

const users = [
  { "department": 1,  "name": "Test", "age": 32, "contact": 242222120, "id": 1 },
  { "department": 1,  "name": "Test", "age": 18, "contact": 242222120, "id": 2 },
  { "department": 1,  "name": "Test", "age": 22, "contact": 242222120, "id": 3 },
  { "department": 2,  "name": "Test", "age": 30, "contact": 242222120, "id": 1 },
  { "department": 2,  "name": "Test", "age": 27, "contact": 242222120, "id": 2 },
  { "department": 3,  "name": "Test", "age": 25, "contact": 242222120, "id": 1 }

];

const departments = users.reduce(function (a, b) {
  const departmentId = b.department;
  delete b.department;
  a[departmentId] = a[departmentId] || [];
  a[departmentId].push(b);
  return a;
}, {});

console.log(departments);
Adrien LAMOTTE
  • 548
  • 4
  • 8
0

you can use the spread operator with destructuring to easily create an object without the department property and use Adrien LAMOTTES answer similar to this:

const departments = users.reduce(function (a, b) {
  let {department, ...c} = b;
  a[department] = a[department] || [];
  a[department].push(c);
  return a;
}, {});
domi
  • 362
  • 4
  • 6