0

I want to compare each element of array real with each element of array number. And if there is any matches push them in array add so I can see them. In this case add must be 2,3,6,10,14 if code is good.

    <!DOCTYPE html>
    <html>
    <head>
    </head>
        <body>
        <script>
        var real=[1,2,3,4,5,6,7,8,10,14,16,233,235,245,2,5,7,236,237];
        var number=[2,3,6,10,12,13,14,172,122,234];
        var add=[];
        for (k=0; k<number.length; k++)
                { 
                    for (w=0; w<real.length; w++)
                        { 
                            if (number[k]==real[w]); 
                                {
                                    add.push(number[k],real[w]);
                                }
                        };

                };
        document.write(add+"<br>");
        </script>

4 Answers 4

2

Here is a short and simple solution using Array.forEach and Array.indexOf functions:

var real = [1,2,3,4,5,6,7,8,10,14,16,233,235,245,2,5,7,236,237],
    number = [2,3,6,10,12,13,14,172,122,234],
    add = [];

real.forEach(function(v) {
    if (number.indexOf(v) !== -1 && this.indexOf(v) === -1) this.push(v);
}, add);

console.log(add);   // [2, 3, 6, 10, 14]
Sign up to request clarification or add additional context in comments.

Comments

0

A more elegant and readable solution:

var matched = [];
real.forEach(function(realNum) {
    number.forEach(function(numberNum) {
        if(realNum == numberNum && matched.indexOf(realNum) === -1) {
            matched.push(realNum);   
        }                     
    });
});

Comments

0

Here's one way you can do it using ES6:

var real = [1, 2, 3, 4, 5, 6, 7, 8, 10, 14, 16, 233, 235, 245, 2, 5, 7, 236, 237];
var number = [2, 3, 6, 10, 12, 13, 14, 172, 122, 234];

var filtered = real.filter(x => number.indexOf(x) > -1);
var unique = new Set(filtered);

document.body.innerHTML = [...unique];

Comments

0

try this way... Sorted the main Array, removed duplicates and the find the common elements from both the arrays.

var main = [1,2,3,4,5,6,7,8,10,14,16,233,235,245,2,5,7,236,237];
var compare = [2,3,6,10,12,13,14,172,122,234];

function compareNumbers(a, b) {
  return a - b;
}
console.log('Sorted Array :', main.sort(compareNumbers) );
// Sorted Array : [1, 2, 2, 3, 4, 5, 5, 6, 7, 7, 8, 10, 14, 16, 233, 235, 236, 237, 245]

Array.prototype.unique = function() {
    var unique = [];
    for (var i = 0; i < this.length; i++) {
        var current = this[i];
        if (unique.indexOf(current) < 0) unique.push(current);
    }
    return unique;
}    
console.log('Unique Array Elements:', main.unique() );
// Unique Array Elements: [1, 2, 3, 4, 5, 6, 7, 8, 10, 14, 16, 233, 235, 236, 237, 245]

function commonElements(arr1, arr2) {
    var common = [];
    for (var i = 0; i < arr1.length; i++) {
        for (var j = 0; j < arr2.length; j++) {
            if (arr1[i] == arr2[j] ) {
                common.push( arr1[i] );
                j == arr2.length; // To break the loop;
            }
        }
    }
    return common;
}

console.log('Common Elements from Both Arrays : ', commonElements(main.unique(), compare.unique()) );
//Common Elements from Both Arrays :  [2, 3, 6, 10, 14]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.