Hey Fellow stackoverflowers, recently i got a new problem accessing array inside array and that inside array containg two objects product and quantity. Now i want to access that inside array how can i do that. Please if you have any solution please share with me:
const arrayOfArrays = [
[
{ product: { name: 'Product 1', price: 10 }, quantity: 1 },
{ product: { name: 'Product 2', price: 20 }, quantity: 2 }
]
];
3
Answers
you need first to take 0th element of outer array, then you can enumerate internal array, like this:
const innerArray = arrayOfArrays[0];
// Accessing the first element of the inner array
const firstElement = innerArray[0];
// Accessing the product name of the first element
const productName = firstElement.product.name;
console.log(productName);
Use double square brackets to access the inner
array
and the desired attribute within eachobject
to access the Product and Quantity objects within thenested array
.Like this, you can access properties from the nested array.