Created
August 11, 2014 03:37
-
-
Save up1/b23cc4aadf2a059431ab to your computer and use it in GitHub Desktop.
Demo :: Manage array with JavaScript
This file contains 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
<script> | |
var arr = []; | |
function onAdd() { | |
arr.push({ | |
key: Math.floor((Math.random() * 100) + 1), | |
cutomerCode: 1234, | |
clientCode: 2344 | |
}); | |
onShowData(); | |
} | |
function onShowData() { | |
console.clear(); | |
for(var i=0; i<arr.length; i++) { | |
console.log(arr[i].key + "=>" + arr[i].cutomerCode); | |
} | |
} | |
function onSortData() { | |
arr.sort( | |
function(a,b) { | |
if (a.key==b.key) return 0; | |
if (a.key<b.key) return -1; | |
return 1; | |
} | |
); | |
onShowData(); | |
} | |
function onClearData() { | |
arr = []; | |
onShowData(); | |
} | |
</script> | |
<button onClick="onAdd()">Add Data</button> | |
<button onClick="onSortData()">Sort Data</button> | |
<button onClick="onClearData()">Clear Data</button> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment