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

122
Views
Ordenar segmento de matriz por propiedad

¿Puede ayudarme a crear una función que cambie el orden de parte de la matriz por propiedad?

Ejemplo:

 const input = [ {firstName: 'Jack1', lastName: 'Snow1'}, {firstName: 'Jack2', lastName: 'Snow2'}, {firstName: 'Jack3', lastName: 'Snow3'}, {firstName: 'Jack4', lastName: 'Snow4'}, {firstName: 'Jack5', lastName: 'Snow5'}, ]; const order = ['Jack3', 'Jack2', 'Jack4']; function orderBySegment(input, order, 'firstName') { } const output = [ {firstName: 'Jack3', lastName: 'Snow3'}, {firstName: 'Jack2', lastName: 'Snow2'}, {firstName: 'Jack4', lastName: 'Snow4'}, {firstName: 'Jack1', lastName: 'Snow1'}, {firstName: 'Jack5', lastName: 'Snow5'}, ];
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

 const input = [ {firstName: 'Jack1', lastName: 'Snow1'}, {firstName: 'Jack2', lastName: 'Snow2'}, {firstName: 'Jack3', lastName: 'Snow3'}, {firstName: 'Jack4', lastName: 'Snow4'}, {firstName: 'Jack5', lastName: 'Snow5'}, ]; const order = ['Jack3', 'Jack2', 'Jack4']; function orderBySegment(input, order, key) { return input.sort((a, b) => { const aValue = a[key], bValue = b[key]; const aIndex = order.indexOf(aValue), bIndex = order.indexOf(bValue); if (aIndex === -1) return 1; else if (bIndex === -1) return -1; else return aIndex - bIndex; }); } console.log(orderBySegment(input, order, 'firstName'));

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!