5

I have two arrays-

var A =  ["a", "b", "c", "a", "b"];
var B = [["a", "b", "c", "a", "b"], ["c", "a", "b", "a", "b"]];
R = [5, 2];

Need a result R = [5, 2] as in first element of B have same element as A and second element have only 2 elements similar at same index.

I tried an approach with map but its failing.

var o =  ["a", "b", "c", "a", "b"];
var rS = [["a", "b", "c", "a", "b"], ["c", "a", "b", "a", "b"]];
var result = Array(rS.length).fill(0);

rS.map((e1,i1,a1)=>{
    e1.map((e2,i2,a2)=>{
        rS[i1][i2] === o[i1] ? result[i1]+=1 : result[i1]+=0;
    })
})

2 Answers 2

6

You could map b with the result of the count if the nested array item who match.

var a =  ["a", "b", "c", "a", "b"],
    b = [["a", "b", "c", "a", "b"], ["c", "a", "b", "a", "b"]],
    result = b.map(values =>
        values.reduce((count, value, index) => count + (value === a[index]), 0)
    );

console.log(result);

Sign up to request clarification or add additional context in comments.

Comments

2

I hope this works:

var result = A.filter(e => B.indexOf(e) !== -1).length === B.length
console.log(result);

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.