对象数组的深拷贝

  • 上代码
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;
  }
});

你可能感兴趣的:(对象数组的深拷贝)