对象删除所有层级id

版本1:


const deleteEntityId = (entity:any) => {
  const handle:any = (data:any) => {
    let _return;
    if(data instanceof Array){
      _return = [];
      for(let i = 0; i < data.length; i++){
        _return.push(handle(data[i]));
      }
      return _return
    }else{
      for(let key in data){
        let value = data[key];
        if(key !== 'id'){
          if(value instanceof Array){
            value = handle(value)
          }else if(value instanceof Object){
            if(['userGroup', ].indexOf(key) === -1){
              value = handle(value)  
            };
          }
        }else{
            delete data[key]
        }
      };
      _return = data

    }
    return _return
  };
  return handle(entity)
};

版本2:

const deepCopyEntityData = (entity: any, ignoreProperties: String[]): any => {
  const data = {...entity, id: undefined};
  
  const propertyNameList = Object.keys(entity);
  propertyNameList.forEach(propertyName => {
    if ((data[propertyName] instanceof Object) &&  ignoreProperties.indexOf(propertyName) < 0) {
      let _return;
      if((data[propertyName] instanceof Array)){
        _return = [];
        for(let i = 0; i < data[propertyName].length; i++){
          typeof(data[propertyName][i]) !== 'string' ?
            _return.push(deepCopyEntityData(data[propertyName][i],ignoreProperties)) :
              _return.push(data[propertyName][i]);
        };
        data[propertyName] = _return;
      }else{
        data[propertyName] = deepCopyEntityData(data[propertyName], ignoreProperties);
      }
    }
  });
  return data;
};

你可能感兴趣的:(对象删除所有层级id)