polyfill.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. Object.entries = Object.entries || function (obj) {
  2. var ownProps = Object.keys(obj),
  3. i = ownProps.length,
  4. resArray = new Array(i); // preallocate the Array
  5. while (i--)
  6. resArray[i] = [ownProps[i], obj[ownProps[i]]];
  7. return resArray;
  8. };
  9. Object.fromEntries = Object.fromEntries || function (arr) {
  10. let obj = {};
  11. arr.forEach(([k, v]) => obj[k] = v)
  12. return obj;
  13. }
  14. if (!Array.prototype.flat) {
  15. Object.defineProperty(Array.prototype, 'flat', {
  16. value: function (depth) {
  17. let res = [],
  18. depthArg = depth || 1,
  19. depthNum = 0,
  20. flatMap = (arr) => {
  21. arr.map((element, index, array) => {
  22. if (Object.prototype.toString.call(element).slice(8, -1) === 'Array') {
  23. if (depthNum < depthArg) {
  24. depthNum++;
  25. flatMap(element);
  26. } else {
  27. res.push(element);
  28. }
  29. } else {
  30. res.push(element);
  31. if (index === array.length - 1) depthNum = 0;
  32. }
  33. });
  34. };
  35. flatMap(this);
  36. return res;
  37. }
  38. });
  39. }