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

145
Views
lista de matriz inversa basada en el valor seleccionado en javascript

Tengo una matriz de objetos que se ve a continuación

 var arr = [{ DocumentNo: '44', FileId: 1, FilenameWithExtension: 'aof.tif', DocTypeId: 1 }, { DocumentNo: '45', FileId: 2, FilenameWithExtension: 'aofd.tif', DocTypeId: 1 }, { DocumentNo: '46', FileId: 3, FilenameWithExtension: 'mdh.tif', DocTypeId: 1 }, { DocumentNo: '47', FileId: 4, FilenameWithExtension: 'zyl.tif', DocTypeId: 1 }, { DocumentNo: '48', FileId: 5, FilenameWithExtension: 'ddd.tif', DocTypeId: 3 }]

Quiero invertir la matriz según lo que seleccione el usuario,

por ejemplo

si el usuario selecciona documentno en orden descendente, la lista anterior debería comenzar con documentno ->(48,47,46,45,44)

si el usuario selecciona fileid en orden ascendente, la lista anterior debe comenzar con FileId ->(1,2,3,4,5)

si el usuario selecciona fileid en orden descendente, la lista anterior debería comenzar con FileId ->(5,4,3,2,1)

y así.

¿Cómo puedo lograr lo mismo? Busqué en Google pero no pude encontrar nada.

about 4 years ago · Santiago Gelvez
2 answers
Answer question

0

Según tengo entendido, desea ordenar esta matriz de objetos según el field y el sorting order pasado por el usuario. En caso afirmativo, aquí está la demostración :

 var arr = [{ DocumentNo: '44', FileId: 1, FilenameWithExtension: 'aof.tif', DocTypeId: 1 }, { DocumentNo: '45', FileId: 2, FilenameWithExtension: 'aofd.tif', DocTypeId: 1 }, { DocumentNo: '46', FileId: 3, FilenameWithExtension: 'mdh.tif', DocTypeId: 1 }, { DocumentNo: '47', FileId: 4, FilenameWithExtension: 'zyl.tif', DocTypeId: 1 }, { DocumentNo: '48', FileId: 5, FilenameWithExtension: 'ddd.tif', DocTypeId: 3 }] const sortingObj = { field: 'FilenameWithExtension', sortBy: 'desc' }; const res = arr.sort((a, b) => { if (sortingObj.field === 'FilenameWithExtension') { if (sortingObj.sortBy === 'aesc') { return (a[sortingObj.field] < b[sortingObj.field]) ? -1 : ((a[sortingObj.field] > b[sortingObj.field]) ? 1 : 0); } else { return (a[sortingObj.field] < b[sortingObj.field]) ? 1 : ((a[sortingObj.field] > b[sortingObj.field]) ? -1 : 0); } } else { return (sortingObj.sortBy === 'aesc') ? a[sortingObj.field] - b[sortingObj.field] : b[sortingObj.field] - a[sortingObj.field] } }); console.log(res);

about 4 years ago · Santiago Gelvez Report

0

Aquí hay un ejemplo de cómo hacer eso con guión bajo

 let arr = [{ DocumentNo: '44', FileId: 1, FilenameWithExtension: 'aof.tif', DocTypeId: 1 }, { DocumentNo: '45', FileId: 2, FilenameWithExtension: 'aofd.tif', DocTypeId: 1 }, { DocumentNo: '46', FileId: 3, FilenameWithExtension: 'mdh.tif', DocTypeId: 1 }, { DocumentNo: '47', FileId: 4, FilenameWithExtension: 'zyl.tif', DocTypeId: 1 }, { DocumentNo: '48', FileId: 5, FilenameWithExtension: 'ddd.tif', DocTypeId: 3 }]; let userInput = "DocTypeId"; let result = _.sortBy(arr, userInput); console.log(result);
 <script src="http://underscorejs.org/underscore.js"></script>
about 4 years ago · Santiago Gelvez 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!