简体   繁体   中英

Spread Operator for array of array

How to perform spread opration on salesuser array object. need to add..salesuserstargetAmount:500, in that object

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

Tried with

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))
  });

Curly braces ( { ) are tricky here because they both mean block of code and JavaScript object so you need to wrap them with () :

 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM