PROWAREtech

articles » current » javascript » ie11 » ie11-prototype-array-flat

JavaScript: IE11 Array.flat() Method/Function

Create the Array.flat() function for Internet Explorer 11.

Still developing for IE11? Well, the flat() method of Array instances creates a new array with all sub-array elements concatenated into it. This example only flattens one level, keep applying it to keep flattening more levels.


if (!Array.prototype.flat) {
	Array.prototype.flat = function () {

		if (this == null) {
			throw new Error('this is null or not defined');
		}

		var O = Object(this);

		var len = O.length >>> 0;
		var arr = [];
		for (var i = 0; i < len; i++) {
			if (Array.isArray(O[i])) {
				arr.concat(O.flat());
			}
			else {
				arr.push(O[i]);
			}
		}

		return arr;
	};
}

PROWAREtech

Hello there! How can I help you today?
Ask any question

PROWAREtech

This site uses cookies. Cookies are simple text files stored on the user's computer. They are used for adding features and security to this site. Read the privacy policy.
ACCEPT REJECT