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

220
Views
¿Cómo puedo reducir a una forma más concisa mi matriz JS?

Existe el siguiente fragmento de código que me gustaría reducir a una forma más concisa. Creo que no tomé la mejor decisión en absoluto. Al mismo tiempo, deshacerse del error, que consiste en limitar la operación del programa de matrices prescritas de antemano en el código. Por favor, dime cómo se puede hacer. Biblioteca Jquery 3.5.1

 function FaendKey(arr){ var Key = []; for (zz = 0; zz < arr.length; zz++) { if (fileSystem[zz].name == arr[0]) { Key.push(zz); break; } } if (arr.length > 2) { var arrKeyOld = fileSystem[Key[0]].items; for (z = 1; z < arr.length-1; z++) { var tickKey = FaendKeyOld(arr[z],arrKeyOld); Key.push(tickKey); arrKeyOld = arrKeyOld[tickKey].items; } } return Key; } function onSuccess(data){ for (k = 0; k < data.d.results.length; k++) { var UrlFull = data.d.results[k].ServerRelativeUrl.split('/'); UrlFull.splice(0,4); var idKey = []; var idKey = FaendKey(UrlFull); if (idKey.length == 1) { fileSystem[idKey[0]].items.push( { name: data.d.results[k].Name, isDirectory: true, __KEY__: data.d.results[k].UniqueId, dateModified: data.d.results[k].TimeLastModified, items: [], } ) } else { if (idKey.length == 2) { fileSystem[idKey[0]].items[idKey[1]].items.push( { name: data.d.results[k].Name, isDirectory: true, __KEY__: data.d.results[k].UniqueId, dateModified: data.d.results[k].TimeLastModified, items: [], } ) } if (idKey.length == 3) { fileSystem[idKey[0]].items[idKey[1]].items[idKey[2]].items.push( { name: data.d.results[k].Name, isDirectory: true, __KEY__: data.d.results[k].UniqueId, dateModified: data.d.results[k].TimeLastModified, items: [], } ) } .. and then a similar structure up to the value idKey.length == 10...
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

puedes usar una función recursiva para eso

algo como esto

 const saveItem = (keys, data, store) => { if(keys.length === 1){ store.items.push(data) return; } const [key, ...rest] = keys return saveItem(rest, data, store[key]) }

de lo que puedes llamarlo así

 function onSuccess(data){ for (k = 0; k < data.d.results.length; k++) { var UrlFull = data.d.results[k].ServerRelativeUrl.split('/'); UrlFull.splice(0,4); var idKey = []; var idKey = FaendKey(UrlFull); const data = { name: data.d.results[k].Name, isDirectory: true, __KEY__: data.d.results[k].UniqueId, dateModified: data.d.results[k].TimeLastModified, items: [], } saveItem(idKey, data, fileSystem) } }
about 4 years ago · Juan Pablo Isaza Report

0

Un caso de uso para el filter :

 var Key = []; for (zz = 0; zz < arr.length; zz++) { if (fileSystem[zz].name == arr[0]) { Key.push(zz); break; }

probar:

 var Key = arr.filter(x => fileSystem[x].name == arr[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!