美文网首页
对象数组的深拷贝

对象数组的深拷贝

作者: 无名程序猿 | 来源:发表于2019-10-24 16:36 被阅读0次
    • 上代码
    const extensions = [
      {
        key: 'resourceTab',
        name: '会议资料',
        module: 'conference_resource',
        icon: 'data',
        id: 0
      },
      {
        key: 'agendTab',
        name: '会议议程',
        module: 'conference_agenda',
        icon: 'ex-agenda',
        id: 1
      },
      {
        key: 'serviceTab',
        name: '会议服务',
        module: 'conference_service',
        icon: 'ex-service',
        id: 2
      },
      {
        key: 'seatingTab',
        name: '座位编排',
        module: 'seating',
        icon: 'ex-seat',
        id: 3
      }
    ];
    const objDeepCopy = function(source) {
      var sourceCopy = source instanceof Array ? [] : {};
      for (var item in source) {
        sourceCopy[item] =
          typeof source[item] === 'object'
            ? objDeepCopy(source[item])
            : source[item];
      }
      return sourceCopy;
    };
    
    const extendsList = objDeepCopy(extensions);
    extendsList.map(item => {
      switch (item.key) {
        case 'resourceTab':
          item.name = '资料';
          break;
        case 'agendTab':
          item.name = '议程';
          break;
        case 'serviceTab':
          item.name = '服务';
          break;
        case 'seatingTab':
          item.name = '排座';
          break;
      }
    });
    

    相关文章

      网友评论

          本文标题:对象数组的深拷贝

          本文链接:https://www.haomeiwen.com/subject/crddvctx.html