object.ts 629 B

12345678910111213141516171819202122232425262728293031
  1. export function setAttr(obj: any, key: string, value: any) {
  2. key.split('.').reduce((obj, key, i, keys) => {
  3. if (i === keys.length - 1) {
  4. obj[key] = value;
  5. }
  6. return obj[key];
  7. }, obj);
  8. }
  9. export function getAttr(obj: any, key: string): any {
  10. return key.split('.').reduce((obj, key) => obj[key], obj);
  11. }
  12. export function clearObject(obj: any) {
  13. if (!obj || typeof obj !== 'object') {
  14. return;
  15. }
  16. if (Array.isArray(obj)) {
  17. obj.length = 0;
  18. }
  19. for (const k in obj) {
  20. obj[k] = undefined;
  21. }
  22. }
  23. export function updateObject(obj: any, v: any) {
  24. clearObject(obj);
  25. Object.assign(obj, v);
  26. }