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

232
Visualizações
For some reason my object rearranges when passed down as props

I'm getting data as an 'array of arrays', so I have to change it to an 'array of objects' so I can use it on my products page.

In the main products page, I am transforming it correctly, and using console.log, I can correctly see the 'array of objects'. After I pass down this 'array of objects' to my product page so I can use the data in the 'array of objects', the individual objects are rearranged and messed up. I've been trying to understand what went wrong but I cannot debug this. Please help.

My products page:

import React, { useState } from "react";
import Product from "./Product";

function Products() {
  //State management for the data pulled using fetch
  // data comes in as an array of arrays
  // so I have to transform into an array of objects
  const [data, setData] = useState([]);

  // using the first array in the 'array of arrays' as keys
  const keys = data.shift();

  // transforming from 'array of arrays' to 'array of objects'
  const dataObj = data.map(elem => ({ 
    [keys[0]]: elem[0], 
    [keys[1]]: elem[1], 
    [keys[2]]: elem[2], 
    [keys[3]]: elem[3], 
  }));

  console.log(dataObj)

  // pulling data from excel sheet
  const getData = () => {
    fetch(
      "https://sheets.googleapis.com/v4/spreadsheets/1DpaMUmqYxGUKnT9BL0uBuCZ6WJ8vd1CLAklqUvkcA8M/values/Sheet1?alt=json&key=AIzaSyD6o4v215zWtw-kOqGVeuLG50pE2QeRZTg",
      {
        method: "GET",
      }
    )
      .then((response) => response.json())
      .then((data) => {
        // console.log(data.value);
        setData(data.values);
        
      });
  };

  
  return (
    <div>
      Hello, you're in the products page
      <button onClick={getData}>click me</button>
      {dataObj.map((prod) => (
        <Product prod={prod} key={prod.id}/>
        
      ))}
      
    </div>
  );
}

export default Products;

My products page

import React from "react";


function Product(props) {
  
  console.log(props.prod);

  
    return (

      <div>
        
      </div>
    
    );
}

export default Product;

picture of my console log

Ignore the empty array pls. The first array is my 'array of objects' and it is arranged properly, but the other parts are the individual objects and they are messed up.

Edit: data.value

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

shift is a mutation. Never ever ever ever ever ever mutate things in react.

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