我有一个状态数组,如下所示:
const [semiForm, setSemiForm] = useState([
{
name: '',
image: '',
exist: false,
mId: null,
mDetails: [
{
line_type: '',
stock_type: '',
gl_categories: '',
planner_number: '',
planner_name: '',
buyer_number: '',
buyer_name: '',
serial_no: '',
lot_process_type: '',
master_planning_family: '',
level_1_category: '',
level_2_category: '',
level_3_category: '',
level_4_category: '',
category_code: '',
unit_of_measure: '',
secondary_uom: '',
purchasing_uom: '',
shipping_uom: '',
production_uom: '',
component_uom: ''
},
],
prepared_by: '',
recipe_no: '',
approved_by: '',
recipe_date: '',
shelf_life: '',
recipe_type: '',
section: '',
approval_date: '',
ty_final_product: '',
usable_trim: '',
ty_aft_chill: '',
wastage: '',
pri_ingr: [
{
item_code: '',
item_description: '',
start_weight: '',
left_weight: '',
actual_qty: '',
uom: '',
yield: '',
strg_container: '',
cook_vessel: '',
prepared_by: ''
},
],
steps: [
{
description: '',
image: ''
},
],
eqp_used: [
{
name: ''
},
],
}
])
我想根据父对象的"name"将一个新对象添加到"pri_ingr"数组中.
我try 使用下面的代码获取父对象的"名称",但即使在设置了状态之后,它也不起作用.
const addPriIngr = (name) => {
console.log(name)
// return
let newPriIngrObj = {
item_code: '',
item_description: '',
start_weight: '',
left_weight: '',
actual_qty: '',
uom: '',
yield: '',
strg_container: '',
cook_vessel: '',
prepared_by: ''
}
semiForm.map((item, index) => {
console.log(item.name);
if (item.name === name) {
return { ...item, pri_ingr: [...item.pri_ingr, newPriIngrObj] }
}
return {
...item
}
})
}
同样,我也将对另一个嵌套的对象数组使用它.
请帮我把这个整理一下.