How to return the difference between two arrays containing strings [duplicate]
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.
javascript arrays
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.
add a comment |
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.
javascript arrays
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
add a comment |
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.
javascript arrays
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
javascript arrays
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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);
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
add a comment |
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"]));
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
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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);
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
add a comment |
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);
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
add a comment |
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);
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);
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
add a comment |
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
add a comment |
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"]));
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
add a comment |
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"]));
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
add a comment |
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"]));
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"]));
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
add a comment |
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
add a comment |
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