How to return the difference between two arrays containing strings [duplicate]










0
















This question already has an answer here:



  • How to get the difference between two arrays in Javascript?

    60 answers



So I let's say I have an array like this:



["one", "two", "three"]


And then I have another array like this:



["one", "two", "three", "four"]


I need my function to compare the two arrays and return "four". For example, I want the function to do nothing when the array does this:



["one", "two"]


But again, I want the function to return the difference when the array goes back to this:



["one", "two", "three"]


I've been playing with array.filter but so far, filter() has done everything but what I need it to. I know I could accomplish this with a complicated forEach() but I'd really like to avoid that.










share|improve this question













marked as duplicate by Alon Eitan, Hassan Imam, Ray Toal, KarelG, adiga Nov 15 '18 at 8:04


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















  • Do you want (1) all the elements in the second array that are not in the first, (2) all the elements in the first array that are not in the second, or (3) all the elements that appear in one array but not the other?

    – Ray Toal
    Nov 15 '18 at 7:39











  • I want the function to compare & return the (additions) difference between two arrays.

    – Egee
    Nov 15 '18 at 7:41











  • The following answer is good.

    – HMR
    Nov 15 '18 at 8:03















0
















This question already has an answer here:



  • How to get the difference between two arrays in Javascript?

    60 answers



So I let's say I have an array like this:



["one", "two", "three"]


And then I have another array like this:



["one", "two", "three", "four"]


I need my function to compare the two arrays and return "four". For example, I want the function to do nothing when the array does this:



["one", "two"]


But again, I want the function to return the difference when the array goes back to this:



["one", "two", "three"]


I've been playing with array.filter but so far, filter() has done everything but what I need it to. I know I could accomplish this with a complicated forEach() but I'd really like to avoid that.










share|improve this question













marked as duplicate by Alon Eitan, Hassan Imam, Ray Toal, KarelG, adiga Nov 15 '18 at 8:04


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















  • Do you want (1) all the elements in the second array that are not in the first, (2) all the elements in the first array that are not in the second, or (3) all the elements that appear in one array but not the other?

    – Ray Toal
    Nov 15 '18 at 7:39











  • I want the function to compare & return the (additions) difference between two arrays.

    – Egee
    Nov 15 '18 at 7:41











  • The following answer is good.

    – HMR
    Nov 15 '18 at 8:03













0












0








0









This question already has an answer here:



  • How to get the difference between two arrays in Javascript?

    60 answers



So I let's say I have an array like this:



["one", "two", "three"]


And then I have another array like this:



["one", "two", "three", "four"]


I need my function to compare the two arrays and return "four". For example, I want the function to do nothing when the array does this:



["one", "two"]


But again, I want the function to return the difference when the array goes back to this:



["one", "two", "three"]


I've been playing with array.filter but so far, filter() has done everything but what I need it to. I know I could accomplish this with a complicated forEach() but I'd really like to avoid that.










share|improve this question















This question already has an answer here:



  • How to get the difference between two arrays in Javascript?

    60 answers



So I let's say I have an array like this:



["one", "two", "three"]


And then I have another array like this:



["one", "two", "three", "four"]


I need my function to compare the two arrays and return "four". For example, I want the function to do nothing when the array does this:



["one", "two"]


But again, I want the function to return the difference when the array goes back to this:



["one", "two", "three"]


I've been playing with array.filter but so far, filter() has done everything but what I need it to. I know I could accomplish this with a complicated forEach() but I'd really like to avoid that.





This question already has an answer here:



  • How to get the difference between two arrays in Javascript?

    60 answers







javascript arrays






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 7:33









EgeeEgee

305




305




marked as duplicate by Alon Eitan, Hassan Imam, Ray Toal, KarelG, adiga Nov 15 '18 at 8:04


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Alon Eitan, Hassan Imam, Ray Toal, KarelG, adiga Nov 15 '18 at 8:04


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • Do you want (1) all the elements in the second array that are not in the first, (2) all the elements in the first array that are not in the second, or (3) all the elements that appear in one array but not the other?

    – Ray Toal
    Nov 15 '18 at 7:39











  • I want the function to compare & return the (additions) difference between two arrays.

    – Egee
    Nov 15 '18 at 7:41











  • The following answer is good.

    – HMR
    Nov 15 '18 at 8:03

















  • Do you want (1) all the elements in the second array that are not in the first, (2) all the elements in the first array that are not in the second, or (3) all the elements that appear in one array but not the other?

    – Ray Toal
    Nov 15 '18 at 7:39











  • I want the function to compare & return the (additions) difference between two arrays.

    – Egee
    Nov 15 '18 at 7:41











  • The following answer is good.

    – HMR
    Nov 15 '18 at 8:03
















Do you want (1) all the elements in the second array that are not in the first, (2) all the elements in the first array that are not in the second, or (3) all the elements that appear in one array but not the other?

– Ray Toal
Nov 15 '18 at 7:39





Do you want (1) all the elements in the second array that are not in the first, (2) all the elements in the first array that are not in the second, or (3) all the elements that appear in one array but not the other?

– Ray Toal
Nov 15 '18 at 7:39













I want the function to compare & return the (additions) difference between two arrays.

– Egee
Nov 15 '18 at 7:41





I want the function to compare & return the (additions) difference between two arrays.

– Egee
Nov 15 '18 at 7:41













The following answer is good.

– HMR
Nov 15 '18 at 8:03





The following answer is good.

– HMR
Nov 15 '18 at 8:03












2 Answers
2






active

oldest

votes


















3














According to the first half of your question--



var a=["one", "two", "three"];
var b=["one", "two", "three", "four"]
var diff=b.filter((word) => !a.includes(word));
console.log(diff);





share|improve this answer























  • Perfect, this is what I was looking for. Darn it, I was so close but I had my logic backwards. Thanks!

    – Egee
    Nov 15 '18 at 7:55











  • Glad to help you!

    – Monica Acha
    Nov 15 '18 at 7:57


















1














Modify the Array prototype:



Array.prototype.diff = function(a) 
return this.filter(function(i) return a.indexOf(i) < 0;);
;





Array.prototype.diff = function(a) 
return this.filter(function(i) return a.indexOf(i) < 0;);
;

console.log(["test1", "test2","test3","test4","test5","test6"].diff(["test1","test2","test3","test4"]));








share|improve this answer























  • Please do not use this answer. Extending the prototype chain of built-in objects is a bad practice and should be done in rare circumstances.

    – KarelG
    Nov 15 '18 at 8:00

















2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














According to the first half of your question--



var a=["one", "two", "three"];
var b=["one", "two", "three", "four"]
var diff=b.filter((word) => !a.includes(word));
console.log(diff);





share|improve this answer























  • Perfect, this is what I was looking for. Darn it, I was so close but I had my logic backwards. Thanks!

    – Egee
    Nov 15 '18 at 7:55











  • Glad to help you!

    – Monica Acha
    Nov 15 '18 at 7:57















3














According to the first half of your question--



var a=["one", "two", "three"];
var b=["one", "two", "three", "four"]
var diff=b.filter((word) => !a.includes(word));
console.log(diff);





share|improve this answer























  • Perfect, this is what I was looking for. Darn it, I was so close but I had my logic backwards. Thanks!

    – Egee
    Nov 15 '18 at 7:55











  • Glad to help you!

    – Monica Acha
    Nov 15 '18 at 7:57













3












3








3







According to the first half of your question--



var a=["one", "two", "three"];
var b=["one", "two", "three", "four"]
var diff=b.filter((word) => !a.includes(word));
console.log(diff);





share|improve this answer













According to the first half of your question--



var a=["one", "two", "three"];
var b=["one", "two", "three", "four"]
var diff=b.filter((word) => !a.includes(word));
console.log(diff);






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 7:42









Monica AchaMonica Acha

333210




333210












  • Perfect, this is what I was looking for. Darn it, I was so close but I had my logic backwards. Thanks!

    – Egee
    Nov 15 '18 at 7:55











  • Glad to help you!

    – Monica Acha
    Nov 15 '18 at 7:57

















  • Perfect, this is what I was looking for. Darn it, I was so close but I had my logic backwards. Thanks!

    – Egee
    Nov 15 '18 at 7:55











  • Glad to help you!

    – Monica Acha
    Nov 15 '18 at 7:57
















Perfect, this is what I was looking for. Darn it, I was so close but I had my logic backwards. Thanks!

– Egee
Nov 15 '18 at 7:55





Perfect, this is what I was looking for. Darn it, I was so close but I had my logic backwards. Thanks!

– Egee
Nov 15 '18 at 7:55













Glad to help you!

– Monica Acha
Nov 15 '18 at 7:57





Glad to help you!

– Monica Acha
Nov 15 '18 at 7:57













1














Modify the Array prototype:



Array.prototype.diff = function(a) 
return this.filter(function(i) return a.indexOf(i) < 0;);
;





Array.prototype.diff = function(a) 
return this.filter(function(i) return a.indexOf(i) < 0;);
;

console.log(["test1", "test2","test3","test4","test5","test6"].diff(["test1","test2","test3","test4"]));








share|improve this answer























  • Please do not use this answer. Extending the prototype chain of built-in objects is a bad practice and should be done in rare circumstances.

    – KarelG
    Nov 15 '18 at 8:00















1














Modify the Array prototype:



Array.prototype.diff = function(a) 
return this.filter(function(i) return a.indexOf(i) < 0;);
;





Array.prototype.diff = function(a) 
return this.filter(function(i) return a.indexOf(i) < 0;);
;

console.log(["test1", "test2","test3","test4","test5","test6"].diff(["test1","test2","test3","test4"]));








share|improve this answer























  • Please do not use this answer. Extending the prototype chain of built-in objects is a bad practice and should be done in rare circumstances.

    – KarelG
    Nov 15 '18 at 8:00













1












1








1







Modify the Array prototype:



Array.prototype.diff = function(a) 
return this.filter(function(i) return a.indexOf(i) < 0;);
;





Array.prototype.diff = function(a) 
return this.filter(function(i) return a.indexOf(i) < 0;);
;

console.log(["test1", "test2","test3","test4","test5","test6"].diff(["test1","test2","test3","test4"]));








share|improve this answer













Modify the Array prototype:



Array.prototype.diff = function(a) 
return this.filter(function(i) return a.indexOf(i) < 0;);
;





Array.prototype.diff = function(a) 
return this.filter(function(i) return a.indexOf(i) < 0;);
;

console.log(["test1", "test2","test3","test4","test5","test6"].diff(["test1","test2","test3","test4"]));








Array.prototype.diff = function(a) 
return this.filter(function(i) return a.indexOf(i) < 0;);
;

console.log(["test1", "test2","test3","test4","test5","test6"].diff(["test1","test2","test3","test4"]));





Array.prototype.diff = function(a) 
return this.filter(function(i) return a.indexOf(i) < 0;);
;

console.log(["test1", "test2","test3","test4","test5","test6"].diff(["test1","test2","test3","test4"]));






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 7:40









Barr JBarr J

6,15911131




6,15911131












  • Please do not use this answer. Extending the prototype chain of built-in objects is a bad practice and should be done in rare circumstances.

    – KarelG
    Nov 15 '18 at 8:00

















  • Please do not use this answer. Extending the prototype chain of built-in objects is a bad practice and should be done in rare circumstances.

    – KarelG
    Nov 15 '18 at 8:00
















Please do not use this answer. Extending the prototype chain of built-in objects is a bad practice and should be done in rare circumstances.

– KarelG
Nov 15 '18 at 8:00





Please do not use this answer. Extending the prototype chain of built-in objects is a bad practice and should be done in rare circumstances.

– KarelG
Nov 15 '18 at 8:00



Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

政党

天津地下鉄3号線