123456789101112131415161718192021222324252627282930313233343536373839404142 |
- Object.entries = Object.entries || function (obj) {
- var ownProps = Object.keys(obj),
- i = ownProps.length,
- resArray = new Array(i); // preallocate the Array
- while (i--)
- resArray[i] = [ownProps[i], obj[ownProps[i]]];
- return resArray;
- };
- Object.fromEntries = Object.fromEntries || function (arr) {
- let obj = {};
- arr.forEach(([k, v]) => obj[k] = v)
- return obj;
- }
- if (!Array.prototype.flat) {
- Object.defineProperty(Array.prototype, 'flat', {
- value: function (depth) {
- let res = [],
- depthArg = depth || 1,
- depthNum = 0,
- flatMap = (arr) => {
- arr.map((element, index, array) => {
- if (Object.prototype.toString.call(element).slice(8, -1) === 'Array') {
- if (depthNum < depthArg) {
- depthNum++;
- flatMap(element);
- } else {
- res.push(element);
- }
- } else {
- res.push(element);
- if (index === array.length - 1) depthNum = 0;
- }
- });
- };
- flatMap(this);
- return res;
- }
- });
- }
|