如何将对象数组值转换为对象中的其他数组值

How to get object array values to other array values within the object?

本文关键字:数组 对象 其他 转换      更新时间:2023-09-26

我有一个带有地图区域的对象。因为我也使用主页区域的所有其他区域。我想用一些东西将主页区域引用到所有其他区域,这样代码更可读、更短。如何使用对象数组主页进行编辑?所以代码变短了。像这样的_images.edit.areas=_images.homepage.areas?或者我应该在一个单独的对象中定义主页吗?

var _images = {
         homepage: {   
            path: 'homepage.jpg',    
            areas: [{      
                coords: '45,143,106,158',   
                text: 'edit',        
                goto: 'edit'       
              }, {
                coords: '45,164,149,180',
                text: 'config',
                goto: 'config' 
              }, {
                coords: '45,181,108,196',
                text: 'setfree',
                goto: 'setfree'
              }]
          },
         edit:{
            path: 'edit.jpg',
            areas: [{     
                coords: '18,131,113,140',  
                text: 'homepage',
                goto: 'homepage'
              }, {
                coords: '45,164,149,180',
                text: 'config',
                goto: 'config'
              }, {
                coords: '45,181,108,196',
                text: 'setfree',
                goto: 'setfree'
              },{ //above everything is like in hompage, below are news areas
             coords: '638,467,652,478',
             text: 'contract',     
             goto: 'contract'
             }]
             },
        }

这取决于情况。通过分配

_images.edit.areas = _images.homepage.areas;

你从主页上做了一个参考来编辑。主页部分或编辑部分的每一次更改都可以在两个对象中使用,因为它们是链接在一起的。

如果你想要,那也没关系,但如果不是,那么我建议使用两个独立的对象。