1

I am attempting to loop over product data where each node of data is in its own index of the array. How can I return all "product-name" or "product-url" values regardless of their index?

My code looks like this

var products = res.data.CO;

products.forEach((product, index) => {
    console.log(product);
});

I am able to return each entry but not able to use dot or bracket notation to only bring back one value from each entry. Any help is much appreciated.

Here is my data https://i.sstatic.net/Zn5oL.png

1
  • Maybe what you're looking for is the map function? Commented Apr 22, 2018 at 21:45

2 Answers 2

1

Use .map to transform one array into another:

const productNames = products.map(product => product['product-name']);
Sign up to request clarification or add additional context in comments.

Comments

0

You can access values using an array like bracket notation

var products = res.data.CO;

products.forEach((product, index) => {
    console.log(product['product-name']);
});

Or if you just want the product names you could do reduce

s using an array like bracket notation

var products = res.data.CO;

// array of product names
products.map(product => product['product-name']);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.