util.js 782 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. const hasOwnProperty = Object.prototype.hasOwnProperty;
  2. export function hasOwn(obj, key) {
  3. return hasOwnProperty.call(obj, key);
  4. };
  5. function extend(to, _from) {
  6. for (let key in _from) {
  7. to[key] = _from[key];
  8. }
  9. return to;
  10. };
  11. export function toObject(arr) {
  12. var res = {};
  13. for (let i = 0; i < arr.length; i++) {
  14. if (arr[i]) {
  15. extend(res, arr[i]);
  16. }
  17. }
  18. return res;
  19. };
  20. export const getValueByPath = function(object, prop) {
  21. prop = prop || '';
  22. const paths = prop.split('.');
  23. let current = object;
  24. let result = null;
  25. for (let i = 0, j = paths.length; i < j; i++) {
  26. const path = paths[i];
  27. if (!current) break;
  28. if (i === j - 1) {
  29. result = current[path];
  30. break;
  31. }
  32. current = current[path];
  33. }
  34. return result;
  35. };