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

267
Views
Javascript: creación de una matriz de diccionario usando variables para claves y valores

Estoy tratando de crear un diccionario a partir de algunas listas usando el método .push().

El resultado que estoy tratando de lograr es:

 { "departTo": { 1: 1.159, 2: 4.156, 3: 9.185, 3: 10.158, 4: 2.158 }, "departTo": { 1: 2.586, 2: 5.518, 3: 11.584, 3: 7.819, 4: 3.991 } }

A continuación se muestra un ejemplo de código de lo que estoy tratando de hacer:

 console.group("Running Script 1..."); var select_array = []; var arrive_from, info var selection_type = ["departTo", "arriveFrom"]; var data_lists_depart = [[1, 1.159], [2, 4.156], [3, 9.185], [4, 10.158], [5, 2.158]]; var data_lists_arrive = [[1, 2.586], [2, 5.518], [3, 11.584], [4, 7.819], [5, 3.991]]; selection_type.forEach(function (selection, i) { console.log("selection", i + ": ", selection); if (selection === "departTo") data_lists = data_lists_depart; else if (selection === "arriveFrom") data_lists = data_lists_arrive; data_lists.forEach(function (data_list, ii) { console.log(" data_list", ii + ": ", data_list); key = data_list[0]; val = data_list[1]; select_array.push({selection: {key: val}}); }) }) console.log("select_array: ", select_array); console.groupEnd("Running Script 1...");

El resultado que obtengo del código anterior es:

 [ {"selection": {"key": 1.159}}, {"selection": {"key": 4.156}}, {"selection": {"key": 9.185}}, {"selection": {"key": 10.158}}, {"selection": {"key": 2.158}}, {"selection": {"key": 2.586}}, {"selection": {"key": 5.518}}, {"selection": {"key": 11.584}}, {"selection": {"key": 7.819}}, {"selection": {"key": 3.991}} ]

Cualquier ayuda para poner esto en el formato que necesito será muy apreciada. Gracias.

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

0

Puede usar Object.fromEntries con array#map para generar su objeto de resultado.

 const selection_type = ["departTo", "arriveFrom"], data_lists_depart = [[1, 1.159], [2, 4.156], [3, 9.185], [4, 10.158], [5, 2.158]], data_lists_arrive = [[1, 2.586], [2, 5.518], [3, 11.584], [4, 7.819], [5, 3.991]], values = [data_lists_depart, data_lists_arrive], result = Object.fromEntries(selection_type.map((name,i) => ([name, Object.fromEntries(values[i]) ]))); console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

La solución obvia

 const departTo = [[1, 1.159], [2, 4.156], [3, 9.185], [4, 10.158], [5, 2.158]]; const arriveFrom = [[1, 2.586], [2, 5.518], [3, 11.584], [4, 7.819], [5, 3.991]]; const result = { departTo: Object.fromEntries(departTo), arriveFrom: Object.fromEntries(arriveFrom) }; console.log(result);
 .as-console-wrapper{min-height: 100%!important; top: 0}

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!