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

135
Vistas
How to get average from json?
import axios from 'axios';
import React, {useState, useEffect} from 'react';


function App() {
  const [students, setStudents] = useState([]);
  **const avg = arr => arr.reduce((a,b) => a + b, 0) / arr.length**

  useEffect(() => {
    axios
      .get("URL")
      .then((data) => setStudents(data.data.students));
  }, []);

  return (
    <>
      {students.map((student) => (
        <div key={student.id}>
           <p><h3> {student.firstName} {student.lastName}</h3></p>
                <p>{student.email}</p>
                <p>{student.company}</p>
                <p>{student.skill}</p>
               
                <p>{avg(student.grades)}</p>
        </div>
      ))}
    </>
  );
}

export default App;


Hello I put a function to get the average value from the json, but it doesn't work. I don't know if the function is wrong or the location of the function is wrong. Can you please help me?

const avg = arr => arr.reduce((a,b) => a + b, 0) / arr.length

Above is the json file format.
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Based on your json data, you'll need to convert the grades to numbers before they can be averaged, which can be done with map before reduce

const avg = arr => arr.map(grade => parseInt(grade)).reduce((a,b) => a + b, 0) / arr.length
about 4 years ago · Juan Pablo Isaza Denunciar

0

This is more suitable

const average = arr => (arr.reduce((previousValue, currentValue) => parseInt(previousValue) + parseInt(currentValue)) / arr.length) || "Error occured at calculation";
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