Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

151
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda