Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.48 KB | None | 0 0
  1. let data = [[1,2,[3]],4];
  2. Array.prototype.isNormalized = function(){
  3. for(let item of this)
  4. if(typeof item === "object")
  5. return false
  6. return true;
  7. }
  8.  
  9. Array.prototype.normalize = function() {
  10. let output = [];
  11. for(let item of this) {
  12. if(typeof item !== "object") {
  13. output.push(item);
  14. }
  15. else {
  16. if (!item.isNormalized()) {
  17. item = item.normalize();
  18. }
  19. output.push(...item)
  20. }
  21. }
  22. return output;
  23. }
  24. let flatArray = data.normalize();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement