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

117
Vistas
Mostrar el precio medio de un libro

Necesito algún código que implemente el cálculo del precio promedio de los libros, tipo ROMAN . El campo de tipo se puede escribir en cualquier caso (romano, ROMAN).

 let books = [ { name: 'Some-1', author: 'Puskin', type: 'roman', price: 204.5 }, { name: 'Some-2', author: 'Tolstoy', type: 'ROMAN', price: 321.0 }, { name: 'Some-3', author: 'Pero', type: 'tale', price: 211.0 }, { name: 'Some-4', author: 'Koelyo', type: 'roman', price: 204.5 }, { name: 'Some-5', author: 'Christie', type: 'Roman', price: 245.5 }, { name: 'Some-6', author: 'Hohol', type: 'tale', price: 366.0 } ];

Así que derivé todos los tipos con este método:

 const getType = books.map(({type}) => type.toLowerCase())

Y además mi problema es que no sé cómo accedí al precio

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Puedes hacerlo en varios métodos.

Puede recorrer la matriz y resumir todos los precios allí y luego dividir su longitud, también verificará si el tipo de objeto actual es igual a roman , usando .toLowerCase()

si desea devolver el número natural, puede usar el método .toFixed()

En bucle

 let books = [ { name: 'Some-1', author: 'Puskin', type: 'roman', price: 204.5 }, { name: 'Some-2', author: 'Tolstoy', type: 'ROMAN', price: 321.0 }, { name: 'Some-3', author: 'Pero', type: 'tale', price: 211.0 }, { name: 'Some-4', author: 'Koelyo', type: 'roman', price: 204.5 }, { name: 'Some-5', author: 'Christie', type: 'Roman', price: 245.5 }, { name: 'Some-6', author: 'Hoho', type: 'tale', price: 366.0 } ] let TotalPrice = 0; let romanPriceLength = 0; for (let i = 0; i < books.length; i++) { if ((books[i].type).toLowerCase() === "roman") { TotalPrice += books[i].price romanPriceLength++ } } console.log(TotalPrice / romanPriceLength) console.log((TotalPrice / romanPriceLength).toFixed(0))

para cada()

 let books = [ { name: 'Some-1', author: 'Puskin', type: 'roman', price: 204.5 }, { name: 'Some-2', author: 'Tolstoy', type: 'ROMAN', price: 321.0 }, { name: 'Some-3', author: 'Pero', type: 'tale', price: 211.0 }, { name: 'Some-4', author: 'Koelyo', type: 'roman', price: 204.5 }, { name: 'Some-5', author: 'Christie', type: 'Roman', price: 245.5 }, { name: 'Some-6', author: 'Hoho', type: 'tale', price: 366.0 } ] let TotalPrice = 0; let romanPriceLength = 0; books.forEach(book => { if((book.type).toLowerCase() === "roman"){ TotalPrice += book.price romanPriceLength ++ } }) console.log(TotalPrice / romanPriceLength) console.log((TotalPrice / romanPriceLength ).toFixed(0))

para... de

 let books = [ { name: 'Some-1', author: 'Puskin', type: 'roman', price: 204.5 }, { name: 'Some-2', author: 'Tolstoy', type: 'ROMAN', price: 321.0 }, { name: 'Some-3', author: 'Pero', type: 'tale', price: 211.0 }, { name: 'Some-4', author: 'Koelyo', type: 'roman', price: 204.5 }, { name: 'Some-5', author: 'Christie', type: 'Roman', price: 245.5 }, { name: 'Some-6', author: 'Hoho', type: 'tale', price: 366.0 } ] let romanPriceLength = 0; let TotalPrice = 0; for(let book of books){ if(book.type.toLowerCase() === "roman"){ TotalPrice += book.price romanPriceLength++ } } console.log(TotalPrice / romanPriceLength) console.log((TotalPrice / romanPriceLength).toFixed(0))

about 4 years ago · Juan Pablo Isaza Denunciar

0

Puedes hacerlo con filter y reduce también:

 let books = [ { name: 'Some-1', author: 'Puskin', type: 'roman', price: 204.5 }, { name: 'Some-2', author: 'Tolstoy', type: 'ROMAN', price: 321.0 }, { name: 'Some-3', author: 'Pero', type: 'tale', price: 211.0 }, { name: 'Some-4', author: 'Koelyo', type: 'roman', price: 204.5 }, { name: 'Some-5', author: 'Christie', type: 'Roman', price: 245.5 }, { name: 'Some-6', author: 'Hoho', type: 'tale', price: 366.0 } ] const romans = books.filter(book => book.type.toLowerCase() == 'roman') const romansTotalPrice = romans.reduce((a, {price}) => a + price, 0) const romansAvaragePrice = (romansTotalPrice / romans.length).toFixed(2); console.log(romansAvaragePrice)

about 4 years ago · Juan Pablo Isaza Denunciar

0

Utilice un bucle for o forEach() para obtener la suma de los precios y el recuento.

 let books = [ { name: 'Some-1', author: 'Puskin', type: 'roman', price: 204.5 }, { name: 'Some-2', author: 'Tolstoy', type: 'ROMAN', price: 321.0 }, { name: 'Some-3', author: 'Pero', type: 'tale', price: 211.0 }, { name: 'Some-4', author: 'Koelyo', type: 'roman', price: 204.5 }, { name: 'Some-5', author: 'Christie', type: 'Roman', price: 245.5 }, { name: 'Some-6', author: 'Hoho', type: 'tale', price: 366.0 } ] let TotalPrice = 0; let count = 0; books.forEach(book => { if (book.type.toLowerCase() == 'roman') { TotalPrice += book.price; count++; } }); console.log((TotalPrice / books.length).toFixed(2))

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