Created
November 4, 2011 12:09
-
-
Save smrchy/1339200 to your computer and use it in GitHub Desktop.
Difference and intelligent update of two arrays
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
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <script class="jsbin" src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.1.7/underscore-min.js"></script> | |
| <meta charset=utf-8 /> | |
| <title>JS Bin</title> | |
| <!--[if IE]> | |
| <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script> | |
| <![endif]--> | |
| <style> | |
| article, aside, figure, footer, header, hgroup, | |
| menu, nav, section { display: block; } | |
| </style> | |
| </head> | |
| <body> | |
| <p id="res1"></p> | |
| <p id="res2"></p> | |
| <script> | |
| // See: http://jsbin.com/osaduy/edit#javascript,html | |
| // | |
| // Find out the difference between an old array (aold) and a new array (anew) | |
| // | |
| // Values that exist in both arrays should not be touched! | |
| // | |
| // Create a toadd array which contains all fields that need to be added | |
| // Create a toremove array which contains all fields that need to be removed | |
| aold = [1,5,6]; | |
| anew = [1]; | |
| toadd = _.difference(anew,aold); | |
| toremove = _.difference(aold,anew); | |
| document.getElementById('res1').innerHTML = 'TOADD:' + JSON.stringify(toadd); | |
| document.getElementById('res2').innerHTML = 'TODEL:' + JSON.stringify(toremove); | |
| </script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment