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

270
Views
¿Cómo encontrar el objeto anterior y siguiente de una matriz para un objeto activo?

No estoy encontrando ninguna solución. En todas partes muestra solo el primer y último elemento de una matriz.

Supongamos que tengo una matriz

 [ { name: "Hero", Id: "hero" }, { name: "About", Id: "about" }, { name: "Proccess", Id: "process" }, { name: "Mission", Id: "mission" }, { name: "Skill", Id: "skill" }, { name: "Service", Id: "service" }, { name: "Work", Id: "work" }, { name: "Contact", Id: "contact" }, ]

Aquí cada objeto tiene un Id. Supongamos que el ID activo es service . Cuando activo Id es service , entonces tengo que encontrarlo anterior y siguiente Id ( skill anterior, siguiente work ). Aquí tengo que encontrar el objeto anterior y el siguiente de acuerdo con la identificación activa. Aquí se puede cambiar el ID activo. Cuando se cambia la identificación activa, entonces tengo que encontrar los cambios en la identificación del objeto anterior y siguiente.

Creo que puedo aclarar la pregunta. Para el mio es dificil. Por favor, ayúdame.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Puede usar array.findIndex para obtener el índice actual, luego - y + eso para obtener el anterior y el siguiente respectivamente. También querrá tener en cuenta los índices fuera de los límites. La función que tengo devuelve undefined si el índice está fuera de los límites.

 const arr = [ { name: "Hero", Id: "hero" }, { name: "About", Id: "about" }, { name: "Proccess", Id: "process" }, { name: "Mission", Id: "mission" }, { name: "Skill", Id: "skill" }, { name: "Service", Id: "service" }, { name: "Work", Id: "work" }, { name: "Contact", Id: "contact" }, ] const getPrevAndNext = (activeID) => { const index = arr.findIndex((a) => a.Id === activeID) if (index === -1) { return undefined } const prev = arr[index - 1] if (!prev) { return undefined } const next = arr[index + 1] if (!next) { return undefined } return [prev, next] } console.log(getPrevAndNext('service'))

about 4 years ago · Juan Pablo Isaza Report

0

Puede usar el bucle for antiguo para iterar sobre la matriz y verificar si Id coincide, luego tome el index actual y agregue -1 para obtener prev y agregue +1 para obtener next . Como se muestra en el siguiente fragmento.

 let array = [ { name: "Hero", Id: "hero" }, { name: "About", Id: "about" }, { name: "Proccess", Id: "process" }, { name: "Mission", Id: "mission" }, { name: "Skill", Id: "skill" }, { name: "Service", Id: "service" }, { name: "Work", Id: "work" }, { name: "Contact", Id: "contact" }, ]; for(let i = 0; i < array.length; i++){ if(array[i].Id === 'service'){ console.log('prev = ' + array[i-1].Id + ' next = ' + array[i+1].Id); } }

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!