I have two arrays with a bunch of number elements.

I want to check in an array that whether that element exists or not in the second array.

Here are my two arrays:


var arr_main     = [1,2,3];
var arr_second   = [2,4,5];

So, I want to check the second array element exist or not in a first main array.

I have also to use a code in angular 2. So, can it be applicable in javascript and angular 2 

Bhaskar Monitor Asked on September 30, 2017 in Programming.
Add Comment
  • 1 Answer(s)
    to check whether an element in the first array exists or not in the second array. We can check the following ways
    
    
    function isElementExists(source,target) {
    var result = source.filter(function(item) {
    return target.indexOf(item) > -1});
        return (result.length > 0);
    }
    // check whether the test_in_array element
    // exists or not in arr_main main array
    var arr_main  = [1,2,3];
    var test_in_array  = [1];
    console.log(isElementExists(arr_main,test_in_array));
    //Result : true
    test_in_array = [1,2,1,1];
    console.log(isElementExists(arr_main,[1,2,1,1]));
    //Result : true
    test_in_array = [4,5];
    console.log(isElementExists(arr_main,[4,5]));
    //Result : false
    Bhaskar Monitor Answered on September 30, 2017.
    Add Comment
  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.