Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

How to write in javascript(w/wth JQuery) to find values that intersect between arrays?

It should be like

var a = [1,2,3]
var b = [2,4,5]
var c = [2,3,6]

and the intersect function should returns array with value {2}. If possible it could applicable for any number of arrays.

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
235 views
Welcome To Ask or Share your Answers For Others

1 Answer

There are many ways to achieve this.

Since you are using jQuery I will suggest use grep function to filter the value that are present in all three array.

var a = [1, 2, 3]
var b = [2, 4, 5]
var c = [2, 3, 6]

var result = $.grep(a, function(value, index) {
  return b.indexOf(value) > -1 && c.indexOf(value) > -1;
})
console.log(result)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...