There is few methods to get difference to other one between two arrays in javascript.
<script>Array.prototype.es_diff = function(e) {
/*
* Get difference between two array
*
* @param (array|required) e - array
* @return (array) Difference between two array
*/
return this.filter(function(i) {return e.indexOf(i) < 0;});
};</script>
This sample code method is very simple. Use javascript filter() function to filter first array. Then try to get values one by one from first array and find out witch values have been already exist on second array. Return first array values, witch are not contain in second array.
Example:
console.log([1,2,3].es_diff([1,2])); output:/ [3]
console.log([1,2,3].es_diff([1,2,4])); output:/ [3]
console.log([1,2,3,3].es_diff([1,2])); output:/ [3,3]
console.log([1,2,3].es_diff([1,2,3])); output:/ []
console.log([1,2,2,3].es_diff([1,2])); output:/ [3]
<script>Array.prototype.esdiff = function(e) {
/*
* Get difference between two array
*
* @param (array|required) e - array
* @return (array) Difference between two array
*/
var tmp = {}, diff =[],args = this || [];
for(i in e){
switch(typeof e[i]){
case 'function': break;
default: tmp[e[i]] = 1; break;
}
}
for(i in args) {
switch (typeof args[i]) {
case 'function': break;
default:
if (!tmp.hasOwnProperty(args[i]) && diff.indexOf(args[i])) {
diff.push(args[i]);
}
break;
}
}
return diff;
};</script>
This sample code method result are look like to first method's result. But it will be removed duplicate values on the result.
Example:
console.log([1,2,3,10].esdiff([1,2])); output:/ [3, 10]
console.log([1,2,3].esdiff([1,2])); output:/ [3]
console.log([1,2,3].esdiff([1,2,4])); output:/ [3]
console.log([1,2,3,3].esdiff([1,2])); output:/ [3]
console.log([1,2,3].esdiff([1,2,3])); output:/ []
console.log([1,2,2,3].esdiff([1,2])); output:/ [3]
<script>Array.prototype.ESDetails = function(e) {
/*
* Get difference between two array
*
* @param (array|required) e - array
* @return (array) Difference between two array
*/
var tmp1 = {},tmp2 = {}, diff ={first:[],common:[],second:[]},args = this || [];
for(i in e){
switch(typeof e[i]){
case 'function': break;
default:
tmp1[e[i]] = e[i];
break;
}
}
for(i in args) {
switch (typeof args[i]) {
case 'function': break;
default:
if(!tmp1.hasOwnProperty(args[i])){
if( diff.first.indexOf(args[i])){
diff.first.push(args[i]);
}
}else{
if( diff.common.indexOf(args[i])){
diff.common.push(args[i])
}
}
tmp2[args[i]] = 1;
break;
}
}
for(i in tmp1){
if(!tmp2.hasOwnProperty(i)){
diff.second.push(tmp1[i]);
}
}
return diff;
};</script>
The sample code is complexes than previous methods. The result will be contain differences between two arrays, separately and also common values between two arrays.
Example:
console.log([1,2,3].ESDetails([1,2])); output:/ {common:[1, 2],first:[3],second:[]}
console.log([1,2,3].ESDetails([1,2,4])); output:/ {common:[1, 2],first:[3],second:[4]}
console.log([1,2,3,3].ESDetails([1,2])); output:/ {common:[1, 2],first:[3],second:[]}
console.log([1,2,3].ESDetails([1,2,3])); output:/ {common:[1, 2,3],first:[],second:[]}
console.log([1,2,2,3].ESDetails([1,2])); output:/ {common:[1, 2,2],first:[3],second:[]}
Who get fresh code scripts from codrate!
Will send your codrate site configuration email in your mail account.
codrate.com is a standard, fast cross browsing and highly versatile site. It is useful for many large number of Program Development Industries. So you can get support form Codrators , who are the codrate's joiners around world to help your program developments, You can answer other codrator's questions. Communicate with them. Share your knowledge with them. Do you have an interest in programming, So publish your articles about programming. It will help to maintain your professional co-profile. Actually codrate.com is not such as a regular web site. It will be gave new experience, best narrow cross-browser view, reduce processing time to receive browsing request, it's mean do not wasting your time to browsing codrate's web pages because it has been upgrade always modern coding ways. So, what do you waiting for ?. Try your own.