Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

152
Views
Transform data from fetch with React

I have retrieved data from an API, and now trying to transform the data to send a POST request. I want to group two User ID's that match, and POST their common cities in a array instead of separate objects.

For example, data I retrieve looks like this:

{
    "events": [
         {
             "city": "city-1",
             "user": "d1177368-2310-11e8-9e2a-9b860a0d9039"
         },
         {
             "city": "city-2",
            "user": "d1177368-2310-11e8-9e2a-9b860a0d9039"
         }
]}

I want my POST request data to look similar to this:

{
    "user": {
        "d1177368-2310-11e8-9e2a-9b860a0d9039": [
            {
                "city": [
                    "city-1",
                    "city-2"
                ]
            }]}}

So far this is my React component for the request:

import React, { useEffect, useState } from "react";
import axios from "../data/axios";

export default function Events({ fetchEvents }) {
  const [events, setEvents] = useState([]);

  useEffect(() => {
    async function fetchData() {
      const requests = await axios.get(fetchEvents);
      setEvents(requests.data.events);
      return requests;
    }
    fetchData();
  }, [fetchEvents]);

//here is my issue:
  function createSessions(user, city) {
    if (user === user) {

    }
  }

Thank you

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Iterate over the events array, reducing it into an object with a user object property. The user object has the user values from the events array elements as key and the cities are pushed into a city array property.

events.reduce(
  (result, el) => {
    if (!result.user[el.user]) {
      result.user[el.user] = [{ city: [] }];
    }
    result.user[el.user][0].city.push(el.city);
    return result;
  },
  { user: {} }
);

const data = {
  events: [
    {
      city: "city-1",
      user: "d1177368-2310-11e8-9e2a-9b860a0d9039"
    },
    {
      city: "city-2",
      user: "d1177368-2310-11e8-9e2a-9b860a0d9039"
    }
  ]
};

const data2 = data.events.reduce(
  (result, el) => {
    if (!result.user[el.user]) {
      result.user[el.user] = [{ city: [] }];
    }
    result.user[el.user][0].city.push(el.city);
    return result;
  },
  { user: {} }
);

console.log(data2);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!