Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

90
Views
Matriz de objetos

Hola chicos espero que alguien me pueda ayudar. Tengo una variedad de objetos

 const users = [ { id: "2222", name: "Peter", age: 20, carts: [ { productId: "1a3va", price: 3.44, quantity: 3, }, { productId: "3adf4", price: 8.44, quantity: 5, }, ], }, { id: "43443", name: "John", age: 24, carts: [ { productId: "2334a", price: 13.44, quantity: 13, }, { productId: "4234d", price: 1.44, quantity: 1, }, ], }, ];

Entonces mi tarea es para la función invoiceCustomer -> devolver el valor total de los artículos en el carrito, para un cliente determinado con parámetros sugeridos. Y para totalOrderValue debería devolver el valor total del pedido.

 function invoiceCustomer(users, id, name, age) {} function totalOrderValue(order) {}

He experimentado con map y reduce, pero obtengo un error de NaN incluso si uso parseInt.

Espero que alguien pueda ayudarme con esto. ¡Gracias!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Creo que esto debería funcionar.

 function totalOrderValue(users, id, name, age) { let user = users.filter( (item, index, array) => { // Used only one criteria (id) for example simplicity return item.id === id; }); if (user.length === 1) { return user[0].carts.reduce((prevValue, item) => { return prevValue + (item.price * item.quantity); }, 0); // Important: initialize prevValue to 0 } else if (user.length === 0) { throw 'User not found'; } else { throw 'Too many users found'; }

}

No puedo ayudarlo con la función totalOrderValue porque no hay orderId para filtrar los datos, pero entiende el uso de métodos de filter y reduce .

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!