How to extend linqify library to recursively flatten array in JavaScript Install linqify package through npm. npm install linqify Create empty file index.js and include linqify as CommonJS module, which is supported in Node. const { Enumerable } = require( "linqify" ); The Flatten function must check if the object is iterable. The exception is a string that is in JavaScript iterable, but can not be treated as such. function isIterable(obj) { if (obj == null ) return false ; if ( typeof obj == "string" ) return false ; return typeof obj[Symbol.iterator] === "function" ; } The Flatten function is then easily implemented, so in the case of an iterable input the function is recursively called and all returned elements are yielded. Otherwise only the input element is yielded. Enumerable.setMethod( "Flatten" , function *() { for ( let t of this ) { if (isIterable(t)) yield* ...
Objave
Prikaz objav, dodanih na 2019