I have an array of objects like this
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",
},
]
How do I filter this array to only get the first two objects? I don't want to filter them by their attributes. I want to filter using their indexes .
productArray[0]and the second is:productArray[1]. If you need to pull these two out into another array:[productArray[0], productArray[1]]. If you definitely need to use.filterusing the index:productArray.filter((x, idx) => idx < 2);productArray[0].smthngandproductArray[1].smthng