Last active
July 7, 2016 10:06
-
-
Save kalpeshhpatel/6015f88b3fefe600e11bd3b6f6d3a958 to your computer and use it in GitHub Desktop.
Javascript Utility Functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function chunk(arr, size) { | |
if(!Array.isArray(arr)) { | |
throw new TypeError("Input is not an array"); | |
} | |
size = size || 1; //default size | |
if(size > arr.length) { | |
return arr; | |
} else { | |
var i = 0; | |
var j = size; | |
var chunkedArray = []; | |
while( i < arr.length) { | |
chunkedArray.push(arr.slice(i,j)); | |
i = j; | |
j += size; | |
} | |
} | |
return chunkedArray; | |
} | |
var a = [123,234,3,4,5,6,7,8,9]; | |
chunk(a,2); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function compact(arr) { | |
if(!Array.isArray(arr)) { | |
throw new TypeError("Input is not an array"); | |
} | |
var op = arr.filter(function(val,ind,arr){ | |
return !!val; | |
}); | |
return op; | |
} | |
var a = [0, 1, false, 2, '', 3]; | |
compact(a); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function array_concat(arr) { | |
if(!Array.isArray(arr)) { | |
throw new TypeError("Input is not an array"); | |
} | |
var argArr = Array.prototype.slice.call(arguments); | |
argArr = argArr.slice(1); | |
return arr.concat(argArr); | |
} | |
var a = [123,234,3,4,5,6,7,8,9]; | |
array_concat(a,1,2,3,[4],[[5]]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function array_fill(arr,value,start,end) { | |
if(!Array.isArray(arr)) { | |
throw new TypeError("Input is not an array"); | |
} | |
start = start || 0; | |
end = end || arr.length; | |
while(start < end) { | |
arr[start] = value; | |
start++; | |
} | |
return arr; | |
} | |
var a = [123,234,3,4,5,6,7,8,9]; | |
array_fill(a,1,2,3); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function unique(arr) { | |
if(!Array.isArray(arr)) { | |
throw new TypeError("Input is not an array"); | |
} | |
var uq = []; | |
var i = 0; | |
for(i = 0; i < arr.length; i++) { | |
uq.indexOf(arr[i]) == -1 && uq.push(arr[i]) | |
} | |
return uq; | |
} | |
var a = [1,2,3,4,4,5,6,6,7]; | |
unique(a); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function unique(arr) { | |
if(!Array.isArray(arr)) { | |
throw new TypeError("Input is not an array"); | |
} | |
var uq = arr.filter(function(val,ind,arr){ | |
return ind == arr.lastIndexOf(val); | |
}); | |
return uq; | |
} | |
var a = [1,2,3,4,4,5,6,6,7]; | |
unique(a); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment