skip to Main Content

I am merging two arrays which is working great. But i am showing the data on a calendar and i need an array to be used as a single line.

My Data:

[
    {
        "pets": [
            {
                "name": "Hound",
                "id": "290dH8Z7nE3kKqMWcsj5"
            },

            {
                "name": "Roger",
                "id": "290dH8Z7nE3kKqMWcsj7"
            }
        ],
        "date": [
            {
                "date": "2023-05-11"
            }
        ],
        "typeofservice": "Service 1",
        "datestart": "2023-05-11",
        "outsideshift": false,
        "organizationId": "vrpMguv6cwf7L9KLEknR",
        "AddressLine1": "7j2UsMaCI1ehnHl5FbMf",
    }
]
   const partialDetails = a3.reduce((res, item) => {
  res.push({ Subject: item.typeofservice , StartTime: item.datestart, IsAllDay: true, CategoryColor: "#1aaa55", Description: "Address: " + item.AddressLine1 })
  return res;
}, []);

Results Currently

[
    {
        "Subject": "Service 1",
        "StartTime": "2023-05-11",
        "IsAllDay": true,
        "CategoryColor": "#1aaa55",
        "Description": "Address: AddressLine1Address"
    }
]

Wanted result the array in pets to be able to go into the subject line. There could be 1 there could be many.

[
    {
        "Subject": "Service 1 - Hound, Roger",
        "StartTime": "2023-05-11",
        "IsAllDay": true,
        "CategoryColor": "#1aaa55",
        "Description": "Address: AddressLine1Address"
    }
]

Ive tried map inside but that creates a new line

3

Answers


  1. Chosen as BEST ANSWER

    ChatGPT fixed it for me -

    const a4 = a3.reduce((accumulator, currentValue) => {
      const petNames = currentValue.pets.map((pet) => `${pet.name}`);
      const separator = petNames.length === 1 ? "" : ", ";
      const petData = {
      
          datestart: currentValue.datestart,
          typeofservice: `${currentValue.typeofservice} - ${petNames.join(separator)}`
      };
      if (!accumulator.some((item) => item.typeofservice === petData.typeofservice)) {
          accumulator.push(petData);
      }
      return accumulator;
    }, []);
    

  2. You can try:

    const a = [
        {
            "pets": [
                {
                    "name": "Hound",
                    "id": "290dH8Z7nE3kKqMWcsj5"
                },
    
                {
                    "name": "Roger",
                    "id": "290dH8Z7nE3kKqMWcsj7"
                }
            ],
            "date": [
                {
                    "date": "2023-05-11"
                }
            ],
            "typeofservice": "Service 1",
            "datestart": "2023-05-11",
            "outsideshift": false,
            "organizationId": "vrpMguv6cwf7L9KLEknR",
            "AddressLine1": "7j2UsMaCI1ehnHl5FbMf",
        }
    ]
    
     const partialDetails = a.reduce((res, item) => {
     const subject = item.typeofservice + ' - '+ item.pets.map(pet=>pet.name).join( ', ');
     
      res.push({ Subject: subject , StartTime: item.datestart, IsAllDay: true, CategoryColor: "#1aaa55", Description: "Address: " + item.AddressLine1 })
      return res;
    }, []);
    
    console.log(partialDetails)
    Login or Signup to reply.
  3. const partialDetails = data.reduce((res, item) => {
        res.push({ Subject: `${item.typeofservice} - ${item.pets.map((el)=>el.name).join(', ')}` , StartTime: item.datestart, IsAllDay: true, CategoryColor: "#1aaa55", Description: "Address: " + item.AddressLine1 })
        return res;
      }, []);
    

    basically the same as previous answer using map and join for the names.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search