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

123
Views
¿Cómo puedo modificar la indexación en un solo objeto en javascript?

Tengo una lista de objetos para almacenar índices así:

 const initialIndexes = { 0: [0,1,2], 1: [0,1] }

En este punto tengo una variable ResultIndex highlightedResultIndex . Da un índice como este:

 highlightedResultIndex = 0 - case 1 highlightedResultIndex = 4 - case 2 highlightedResultIndex = 2 - case 3 ...

Y finalmente trato de obtener los siguientes resultados highlightedResultIndex

 For the case 1: result = { 0: 0, // index from initialIndexes 1: -1 } For the case 2: result = { 0: -1, 1: 1 // index from initialIndexes } For the case 3: result = { 0: 2 // index from initialIndexes 1: -1 }

Tal vez haya un método fácil o un cierto algoritmo para esto, pero no sabía cómo investigarlo, así que quería escribirlo aquí. Me alegraría si me ayudas.

EDITAR:

Permítanme poner aquí algunos de los ejemplos

 // This is data from the server side. const initialIndexes = { 0: [0,1,2,3], 1: [0,1,2], 2: [0,1,2], 3: [0,1,2,3,4] } // So, I need to index object like this by highlightedResultIndex variable // highlightedResultIndex = 0 { 0:0, 1:-1, 2:-1, 3:-1 } // highlightedResultIndex = 6 { 0: -1, 1:2, 2:-1, 3:-1 } // highlightedResultIndex = 10 { 0: -1, 1:-1, 2:-1, 3:0 }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Puede tomar el valor del índice deseado y reducirlo por la longitud de las matrices hasta que encaje en una matriz.

 const initialIndexes = { 0: [0, 1, 2, 3], 1: [0, 1, 2], 2: [0, 1, 2], 3: [0, 1, 2, 3, 4] }, getIndices = n => Object.fromEntries(Object .entries(initialIndexes) .map(([key, array]) => { const value = n >= 0 && n < array.length ? array[n] // or just n : -1; n -= array.length; return [key, value]; }) ); console.log(getIndices(0)); // { 0: 0, 1: -1, 2: -1, 3: -1 } console.log(getIndices(6)); // { 0: -1, 1: 2, 2: -1, 3: -1 } console.log(getIndices(10)); // { 0: -1, 1: -1, 2: -1, 3: 0 }
 .as-console-wrapper { max-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!