数组数组的扩展运算符

Spread Operator for array of array

如何对 salesuser 数组对象执行传播操作。需要在该对象中添加 ..salesuserstargetAmount:500

  {        
    "station": [
        {
            "stationname":"chennai",
            "stationtargetAmount":500,
            "salesusers":[
                {
                " salesusersname":"tester", 
                },         
         ]
        },
    {
            "stationname":"chennai22222",
           "stationtargetAmount":500,
            "salesusers":[
                {
                " salesusersname":"tester222222", 
                },         
         ]
        }
    ]
 }

尝试过

let  salesusertargetamt = "100"

 this.setState({
   salesusers: this.state.station.map((items) => {
   items.salesusers.map(el => (el.salesusersname !== "" ? { ...el, salesusertargetamt } : el))
  });
let  salesusertargetamt = "100"

 this.setState({
   salesusers: this.state.station.map((items) => {
    return items.salesusers.map(el => (el.salesusersname !== "" ? { ...el, stationtargetAmount: salesusertargetamt } : el))
  });

大括号 ({) 在这里很棘手,因为它们都表示代码块和 JavaScript 对象,因此您需要用 ():

将它们括起来

let  salesusertargetamt = "100";

 let obj = {        
    "station": [
        {
            "stationname":"chennai",
            "stationtargetAmount":500,
            "salesusers":[
                {
                " salesusersname":"tester", 
                },         
         ]
        },
    {
            "stationname":"chennai22222",
           "stationtargetAmount":500,
            "salesusers":[
                {
                " salesusersname":"tester222222", 
                },         
         ]
        }
    ]
 };
 
 let result = obj.station.map(items => ({
    ...items,
    salesusers: items.salesusers.map(user => (user.salesusersname !== "" ? {...user, salesusertargetamt} : user ))
 }))
 

 console.log(result);