当前位置:网站首页>Day 245/300 JS foreach data cannot be updated to the object after multi-layer nesting

Day 245/300 JS foreach data cannot be updated to the object after multi-layer nesting

2022-07-06 06:40:00 Arden Zhao

( One ) demand

Add objects and edit objects , The default value of the attribute value under the business scenario should be distinguished , Set different default values .

( Two ) Realization

1、forEach Failed to update data

Try it at first , After getting the asynchronous request , Direct data processing , But it failed , Choose different business scenarios on the other side , Can't update .

  • Deep copy of complex object data , I tried. No
  • nextTick To the next micro task asynchronous queue , I tried. No
  • setTimeout To the next macro task asynchronous queue , Is it OK to try

A slight breakdown of mood , Just not ;

2、 Further investigation

The discovery is after selecting the business scenario , Data will be re assigned ; And choose the old object used by reassignment .
Although I still don't understand it until now , Why? , After data rendering , Still not rendered to the original object , Here's a record of . I'll add later .

this.toolsList.forEach(function (item) {
    
  item.checked= true
  if (_this.newState) {
    
    if (item.tool_name === 'other') {
    
      const attributes = item.attributes
      attributes.forEach((attr) => {
    
        if (attr.name === 'edit_object_model') {
    
          console.log('attr2', attr)
          attr.value = '10'
        }
      })
    }
  }
})

Write it at the end

On the way to study , Often slack off

《 There are students who want to learn technology and need supervision ~》
https://mp.weixin.qq.com/s/FyuddlwRY7DsHUejCjiVug

原网站

版权声明
本文为[Arden Zhao]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060628285074.html