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

137
Views
Cómo el mapa de valores de Node JS se suma a K

Dada una lista de números const arr = [{ key1: 10 }, { key2: 5 }, { key3: 7 }, { key4: 17 }]; y un numero k dicen const k = 17; , devuelve si dos números cualesquiera de la lista suman k. Por ejemplo, dado [10, 15, 3, 7] y k = 17, deberíamos devolver True, ya que 10 + 7 = 17.

Escribí debajo del código usando key,values en Map, pero parece que no puedo obtener la solución, ¿alguien puede sugerir qué cambio de código se necesita a continuación? Quiere resolverlo especialmente usando Maps

 const arr = [{ key1: 10 }, { key2: 5 }, { key3: 7 }, { key4: 17 }]; const k = 17; let addInput = new Map(arr.flatMap((x) => Object.entries(x))); addInput.forEach((v) => { for (var i = 0; i < addInput.size; i++) { if (v != addInput[i]) { if (addInput[i] + v == k) { console.log(`${v} + ${addInput[i]} = ${k} (true)`); } } } });
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

El enfoque no está claro con su código: debe usar Set para eliminar duplicados, no Map , y debe usar el operador de map para extraer los valores del objeto, tampoco puede acceder al operador de mapa con índice numérico como matriz.

 const arr = [{ key1: 10 }, { key2: 5 }, { key3: 7 }, { key4: 17 }]; const k = 17; // using set to remove duplicate let valueSet = new Set(arr.flatMap((x) => Object.values(x))); // using array to iterate easily, convert by destruturing let valueArray = [...valueSet]; valueArray.forEach((v1, i1) => { for (let i2 = i1 + 1 ; i2 < valueArray.length; i2++) { if ((v1 + valueArray[i2]) === k) { console.log(`${v1} + ${valueArray[i2]} = ${k} (true)`); } } });

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!