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

98
Views
JavaScript Calcular matriz de objetos

Tengo algunas dificultades para calcular dentro de a para cada función con JavaScript. Tengo una matriz de objetos que se parece a esto:

 [ { "attribute_operator": "*", "attribute_value_price": 3 }, { "attribute_operator": "+", "attribute_value_price": 2 }, { "attribute_operator": "*", "attribute_value_price": 2 } ]

Como puede ver, cada valor tiene un operador y hay un precio inicial, por lo que el cálculo real debería verse así (si el precio inicial es 6, por ejemplo):

6 * 3 + 2 * 2

Entonces la salida debería ser en ese caso 40.

Sé cómo hacerlo por suma, pero ¿cómo implementar mi operador fácilmente?

intentado como:

 let total = starting_price.value calculate_objects.forEach((a: { attribute_value_price: number; }) => { total += a.attribute_value_price; }); console.log(total);
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Este es un buen caso para reducir. El valor inicial es ese starting_price , y cada giro del bucle aplica una pequeña función relacionada con el operando...

 const starting_price = 6; const operations = [{ "attribute_operator": "*", "attribute_value_price": 3 }, { "attribute_operator": "+", "attribute_value_price": 2 }, { "attribute_operator": "*", "attribute_value_price": 2 } ]; const operators = { '*': (a, b) => a * b, '+': (a, b) => a + b, }; const result = operations.reduce((acc, el) => { let operator = operators[el.attribute_operator]; let operand = el.attribute_value_price; return operator(acc, operand); }, starting_price); console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

Puede usar cadenas literales con la función eval. La función eval es útil cuando desea calcular una expresión.

 const obj = [ { "attribute_operator": "*", "attribute_value_price": 3 }, { "attribute_operator": "+", "attribute_value_price": 2 }, { "attribute_operator": "*", "attribute_value_price": 2 } ] let starting_price = 6; let total = starting_price; obj.forEach((a) => { total =eval(`${total}${a.attribute_operator}${a.attribute_value_price}`) ; }); console.log(total);

Advertencia: ejecutar JavaScript desde una cadena es un GRAN riesgo de seguridad.

Con eval() , el código malicioso puede ejecutarse dentro de su aplicación sin permiso.

Con eval() , el código de terceros puede ver el alcance de su aplicación, lo que puede generar posibles ataques.

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!