I want to make a map and populate an array with the datas of this map, The array "rows" going to be used in a DataGrid component of material-ui

Viewed 474

But when I do a console.log(rows) I got an undefined list.

let rows: Array<{ id: number }> = []

rows = products?.map((product) =>  
  { id: product.product_id }
) 

When I try to do without map it works, like:

let rows = [
 {id: 1},
 {id: 2},
 {id: 3},
]

The array "rows" going to be used in a DataGrid component of material-ui, so I need populate the array rows with map to stay equals the array above.

1 Answers

The map callback function is not returning anything. Here are two ways to fix this:

  1. Adding circular braces around the object to be returned
rows = products?.map(product => ({ id: product.product_id }));
  1. Using return
rows = products?.map(product => { 
  return { 
    id: product.product_id 
  } 
});
Related