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

120
Views
Matriz de objeto en un objeto anidado para cada valor en la matriz

Tratando de convertir una matriz de objetos en un objeto anidado. ¿Hay un buen método para esto? y ¿cómo lo hago dependiendo de la longitud de la matriz?

Funciona pero no es universal: https://codesandbox.io/s/thirsty-roentgen-3mdcjv?file=/src/App.js

Lo que tengo:

 sorting: [ { "id": "HighestDegree", "options": [ "HighSchool", "Undergraduate", "Bachelor", "Master", "Doctor" ] }, { "id": "gender", "options": [ "male", "female" ] } ]

Lo que quiero:

 value: { "Region": "Oklahoma", "HighestDegree": { "HighSchool": { "male": null, "female":null }, "Undergraduate":{ "male": null, "female":null } //and so on... } }

El código a continuación funciona, pero está codificado para solo dos opciones diferentes. Quiero que pueda anidar la longitud de la matriz. Entonces, digamos que otro objeto era la edad, sería {"HighSchool":{male:{"<25":null,"25-35":null}}} etc.

 function testSortingArray() { let sorting = [ { id: "HighestDegree", options: ["HighSchool", "Undergraduate", "Bachelor", "Master", "Doctor"] }, { id: "gender", options: ["male", "female"] } ]; let GoalArray = {}; if (sorting.length > 0) { sorting[0].options.map((firstArray) => { let currObject = {}; sorting[1].options.map((secondOption) => { currObject[secondOption] = null; }); GoalArray[firstArray] = currObject; }); } return GoalArray; } console.log(testSortingArray());
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Puedes hacerlo con una función recursiva.

La siguiente función reduce cada matriz de options a un objeto, y luego continúa llenando ese objeto si quedan elementos rest de la matriz de sorting original.

 const fn = ([{ options }, ...rest]) => options.reduce((a, v) => ({ ...a, [v]: rest.length ? fn(rest): null }), {}); const result = fn(sorting);

Además del método reduce() , el código anterior utiliza la desestructuración de objetos y matrices y la sintaxis extendida .


Fragmento completo:

 const sorting = [{ "id": "HighestDegree", "options": [ "HighSchool", "Undergraduate", "Bachelor", "Master", "Doctor" ] }, { "id": "gender", "options": [ "male", "female" ] }, { "id": "age", "options": [ "<25", "25-35" ] }]; const fn = ([{ options }, ...rest]) => options.reduce((a, v) => ({ ...a, [v]: rest.length ? fn(rest): null }), {}); const result = fn(sorting); console.log(result);

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!