Skullomania
Skullomania

Reputation: 2215

How do I return the similar values between 2 arrays, and in the order of the second?

I have 2 arrays. I am trying to return the similar values between the 2 but in the order of the second. For example, take a look at the two arrays:

array1 = ['a', 'b', 'c']
array2 = ['b', 'c', 'a', 'd']

What I would like to return is this:

sim = ['b', 'c', 'a']

Here is a link to what I am trying to accomplish. Currently the script is faulty and not catching the corner case.

Upvotes: 2

Views: 59

Answers (5)

Klodian
Klodian

Reputation: 713

try this example here similar-values betwe en two arrays

var a1 = ['a' ,'b'];

var a2 = ['a' ,'b' ,'c'];

var result = arr_sim(a1,a2);// call method arr_sim

console.log(result);

function arr_sim (a1, a2) {

    var similar = [];

    for( var i = 0 ; i <a1.length ; i++ ){ // loop a1 array

        for( var j = 0 ; j <a2.length ; j++ ){ // loop a2 array

            if( a1[i] ==  a2[j] ){ // check if is similar

                similar.push(a1[i]); // add to similar array

                break; // break second loop find  that is similar

            } // end if

        } // end second lopp

    } // end first loop

    return similar; // return result

} // end function

Upvotes: 0

Nina Scholz
Nina Scholz

Reputation: 386654

You could use a Set for array1 use Array#filter array2 by checking the set.

var array1 = ['a', 'b', 'c'],
    array2 = ['b', 'c', 'a', 'd'],
    theSet = new Set(array1),
    result = array2.filter(v => theSet.has(v));
    
console.log(result);

Some annotations to your code:

function arr_sim (a1, a2) {
    var //a = {},                         // take an object as hash table, better
        a = Object.create(null),          // a really empty object without prototypes
        sim = [],
        i;                                // use single declaration at top

    for (i = 0; i < a1.length; i++) {     // iterate all item of array 1
        a[a1[i]] = true;
    }

    for (var i = 0; i < a2.length; i++) {
        if (a[a2[i]]) {
            sim.push(a2[i]);              // just push the value
        }
    }
    return sim;
}

console.log(arr_sim(['a', 'b', 'c'], ['b', 'c', 'a', 'd']));

Upvotes: 8

Lucas Alencar
Lucas Alencar

Reputation: 350

Try this

const arr_sim = (a1, a2) => a2.filter(a => a1.includes(a))

console.log(arr_sim(['a', 'b', 'c'], ['b', 'c', 'a', 'd']));

Upvotes: 1

Nishant Dixit
Nishant Dixit

Reputation: 5522

I think this is what you are looking for?

function arr_sim (a1, a2) {
a1 = Array.isArray(a1)?a1:typeof a1 == "string"?a1.split(""):false;
a2 = Array.isArray(a2)?a1:typeof a2 == "string"?a2.split(""):false;
if(!a1 || !a2){
alert("Not valid values");
return;
}

var filterArray = a1.filter(function(val){
return a2.indexOf(val) !== -1;
})

    return filterArray;
}

console.log(arr_sim(['a', 'b'], ['b', 'a', 'c', 'd']));
console.log(arr_sim("abcd", "abcde"));
console.log(arr_sim("cxz", "zcx"));

Upvotes: 1

KevBot
KevBot

Reputation: 18898

You can iterate array2 with a filter, and check if the value is contained in array1:

let array1 = ['a', 'b', 'c'];
let array2 = ['b', 'c', 'a', 'd'];

let sim = array2.filter((entry) => {
  return array1.includes(entry);
});

console.log(sim);

Upvotes: 4

Related Questions