0

Use Json

Hi This is my json code: { "products": [ { "title": "book1", "price": 514 }, { "title": "book2", "price": 541 } ] } And this is my code: let products = [] axios.get("http://localhost:3000/products") .then(res => { res.data.forEach(element => { products.push({ title: element.title, price: element.price }) }); }) console.log(products) How access products's title?

30th Mar 2020, 6:02 PM
Mehdi Najafi
Mehdi Najafi - avatar
3 Antworten
0
products is an array you can use for loop or forEach to loop over products or use index to access its element. products[0] and products[1] is the object. Their title can be retrieved with products[0].title or products[0]["title"]
30th Mar 2020, 6:05 PM
Gordon
Gordon - avatar
0
It doesn't work...
30th Mar 2020, 6:09 PM
Mehdi Najafi
Mehdi Najafi - avatar
0
Can you put it in a code in code playground? Let me edit to show you Please show your code here so that we can help you https://www.sololearn.com/post/75089/?ref=app
30th Mar 2020, 11:16 PM
Gordon
Gordon - avatar