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

Time:09-10

I have a array of object like this :

{
    "data": [
        {
            "id": 1,
            "from": "2022-08-01",
            "to": "2022-08-05",
            "description": "test 1",
            "files": [
                {
                    "id": 1,
                    "hospital_name": "hospital 11",
                    "hospital_id": 11,
                    "period_id": 1
                },
                {
                    "id": 2,
                    "hospital_name": "hospital 11",
                    "hospital_id": 11,
                    "period_id": 1
                }
            ]
        },
        {
            "id": 2,
            "from": "2022-08-06",
            "to": "2022-08-10",
            "description": "test 2",
            "files": [
                {
                    "id": 3,
                    "hospital_name": "hospital 12",
                    "hospital_id": 12,
                    "period_id": 2
                },
                {
                    "id": 4,
                    "hospital_name": "hospital 12",
                    "hospital_id": 12,
                    "period_id": 2
                }
            ]
        }
    ]
}

I want to convert the array to be like this :

{
    "data": [
        {
            "id": 1, // this is period id
            "hospital_name": "hospital 11",
            "hospital_id": 11,
            "from": "2022-08-01",
            "to": "2022-08-05",
            "description": "test 1"
        },
        {
            "id": 2,
            "hospital_name": "hospital 12",
            "hospital_id": 12,
            "from": "2022-08-06",
            "to": "2022-08-10",
            "description": "test 2"
        }
    ]
}

So I expect the results like that

I try to type my code like this :


data.flatMap((period) =>
    period.files.map((file) => ({
        id: period.id,
        hospital_name: file.hospital_name,
        hospital_id: file.hospital_id,
        from: period.from,
        to: period.to,
        description: period.description,
    }))
)

But the problem is my code show duplicate id

How can I solve my problem?

Note :

Every period only has one hospital id

CodePudding user response:

You can filter the last result like:

const j = {
  "data": [{
      "id": 1,
      "from": "2022-08-01",
      "to": "2022-08-05",
      "description": "test 1",
      "files": [{
          "id": 1,
          "hospital_name": "hospital 11",
          "hospital_id": 11,
          "period_id": 1
        },
        {
          "id": 2,
          "hospital_name": "hospital 11",
          "hospital_id": 11,
          "period_id": 1
        }
      ]
    },
    {
      "id": 2,
      "from": "2022-08-06",
      "to": "2022-08-10",
      "description": "test 2",
      "files": [{
          "id": 3,
          "hospital_name": "hospital 12",
          "hospital_id": 12,
          "period_id": 2
        },
        {
          "id": 4,
          "hospital_name": "hospital 12",
          "hospital_id": 12,
          "period_id": 2
        }
      ]
    }
  ]
}



console.log(j.data.flatMap((period) =>
      period.files.map((file) => ({
        id: period.id,
        hospital_name: file.hospital_name,
        hospital_id: file.hospital_id,
        from: period.from,
        to: period.to,
        description: period.description,
      }))
    ).filter((value, index, self) =>
      index === self.findIndex((t) => (
        t.id === value.id
      ))))

Reference:

CodePudding user response:

You don't need flatMap, just run map on periods and for each period, pick the first file to extract hospital_id and hospital_name like this:

data.map(period => {
  const file = period.files[0];
  return {
    id: period.id,
    hospital_name: file.hospital_name,
    hospital_id: file.hospital_id,
    from: period.from,
    to: period.to,
    description: period.description,
  }
});

CodePudding user response:

Since the objects in the files array are identical (other than the id) you can destructure the properties from the data array, and (within that) the first object of each files array, and return a new object.

const data={data:[{id:1,from:"2022-08-01",to:"2022-08-05",description:"test 1",files:[{id:1,hospital_name:"hospital 11",hospital_id:11,period_id:1},{id:2,hospital_name:"hospital 11",hospital_id:11,period_id:1}]},{id:2,from:"2022-08-06",to:"2022-08-10",description:"test 2",files:[{id:3,hospital_name:"hospital 12",hospital_id:12,period_id:2},{id:4,hospital_name:"hospital 12",hospital_id:12,period_id:2}]}]};

const out = data.data.map(obj => {

  const {
    from,
    to,
    files: [{ period_id, id, ...rest }],
    description
  } = obj;
  
  return { id: period_id, from, to, description, ...rest }

});

console.log(out);

Additional information

  • Related