cant access object attributes from js array

Ushan Fernando

i have this simple code to get some objects from api and add it to an array

 const res = await fetch("http://localhost:5000/cart/carts/"+this.state.user);
  const data = await res.json();
 let data1 = [];
    data.forEach(async item => {
        const res1 = await fetch("http://localhost:5000/storemanger/products/"+item.product);
          const object = await res1.json();
          data1.push(object);
      });
console.log(data1)

output of console log

output chrome console

but if try to access first element like

console.log(data1[0])

output is "undefined "

i want to access the productname of objects inside the array

Denis Tsoi

If you prefer async/await syntax alternative:

You can use for await ... of syntax.

Using json placeholder as an example

const response = []

const data = [1,2,3]

for await (item of data) {
  const res = await fetch("https://jsonplaceholder.typicode.com/todos/" + item);
  const obj = await res.json()
  response.push(obj)
}

console.log(response) //

response at the end yields

[
  {userId: 1, id: 1, title: "delectus aut autem", completed: false},
  {userId: 1, id: 2, title: "quis ut nam facilis et officia qui", completed: false},
  {userId: 1, id: 3, title: "fugiat veniam minus", completed: false}
]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Cant access object value from within an array

Cant access single object in array?

cant access single object in array in ejs

I cant access an object key in JS

How to access attributes of object literals in javascript array

Cant access member of object

Print an array of object attributes from an array of objects

Function which access an object picked from an array and returns it to have attributes modified

How to access attributes of php object inside a array of php objects from javascript

JS console log value in array of object attributes

Cant use variable from React Context to access value of object

Access object attributes when the object is in an array C#

Chart JS can't access data from array within an object

how to access nested object property from an array in vue js

References to object attributes from array values

I cant' access my data from database'data in array

How can I insert attributes of an object from an array of objects into SQLServer using Node JS?

Cant access JavaScript object property

Cant access an object property in laravel

Cant access JSON object property

Cant remove field from JSON object - Node JS

Cant access fetched elements of an array

Trying to access instantiated object in 2d array Unity c#. Cant access or change elements?

Cant get attributes from nested objects

Unable to get the attributes of an object from object array saved in Mongodb document

How to add object attributes in array from another array in Ruby

How to access custom attributes from event object in React?

Access instance object attributes from instance.stack()

How do you access attributes of an object queried from Mongo in Meteor