我想制作一张地图并用这张地图的数据填充一个数组,数组 "rows" 将在 material-ui 的 DataGrid 组件中使用

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

但是当我执行 console.log(行) 时,我得到了一个未定义的列表。

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

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

当我尝试在没有地图的情况下工作时,它会起作用,例如:

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

material-ui 的 DataGrid 组件中将使用数组“rows”,所以我需要用 map 填充数组 rows 以保持等于上面的数组。

map 回调函数未返回任何内容。这里有两种方法可以解决这个问题:

  1. 在要返回的对象周围添加圆括号
rows = products?.map(product => ({ id: product.product_id }));
  1. 使用return
rows = products?.map(product => { 
  return { 
    id: product.product_id 
  } 
});