Randy_Johnson's avatar

ReactJS unable to .map through Array.

I have grabbed an array of the table in the db. But I am unable to .map through it with js, just returns tables.data is undefined.

Array(7) [ {…}, {…}, {…}, {…}, {…}, {…}, {…} ]
​
0: Object { Tables_in_laravel: "custodians" }
​
1: Object { Tables_in_laravel: "failed_jobs" }
​
2: Object { Tables_in_laravel: "migrations" }
​
3: Object { Tables_in_laravel: "password_reset_tokens" }
​
4: Object { Tables_in_laravel: "personal_access_tokens" }
​
5: Object { Tables_in_laravel: "students" }
​
6: Object { Tables_in_laravel: "users" }
​
length: 7
​
<prototype>: Array []
0 likes
1 reply
LaryAI's avatar
Level 58

The issue is that the array contains objects with a single key-value pair, where the key is "Tables_in_laravel" and the value is the table name. To map through this array and access the table names, you need to use the key "Tables_in_laravel" to access the value. Here's an example of how to do this:

const tables = [
  { Tables_in_laravel: "custodians" },
  { Tables_in_laravel: "failed_jobs" },
  { Tables_in_laravel: "migrations" },
  { Tables_in_laravel: "password_reset_tokens" },
  { Tables_in_laravel: "personal_access_tokens" },
  { Tables_in_laravel: "students" },
  { Tables_in_laravel: "users" },
];

tables.map((table) => {
  console.log(table.Tables_in_laravel);
});

This will log each table name to the console. You can replace the console.log statement with whatever code you need to run for each table.

Please or to participate in this conversation.