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

169
Views
La mejor manera de filtrar una matriz de objetos por índice en JavaScript

Tengo una variedad de objetos como este.

 export const productArray = [ { imgUrl: images.beautifulinwhite, productName: "White Flowers", oldPrice: "$45.99", newPrice: "$35.99", }, { imgUrl: images.blueandpinkjeans, productName: "Blue and Pink Jeans", oldPrice: "$57.99", newPrice: "$40.99", }, { imgUrl: images.girlinyellow, productName: "Yellow Tshirt", oldPrice: "$53.99", newPrice: "$37.99", }, { imgUrl: images.ladyinblack, productName: "Black Hoodie", oldPrice: "$40.99", newPrice: "$33.99", }, ]

¿Cómo filtro esta matriz para obtener solo los dos primeros objetos? No quiero filtrarlos por sus atributos. Quiero filtrar usando sus índices.

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

0

La mejor manera de filtrar la matriz por índice es usando el método slice() :

 const productArrayFiltered = productArray.slice(0, 2);

Desde MDN :

El método slice() devuelve una shallow copy de una parte de una matriz en un nuevo objeto de matriz seleccionado de principio a fin (no se incluye el final), donde el inicio y el final representan el índice de los elementos de esa matriz. The original array will not be modified.

Nota al margen: es importante recordar que el método devuelve una nueva matriz que debe asignarse a una nueva constante o variable.

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!