Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

153
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda