-1

I have two arrays and I would like to compare them and return all the values that are on both, even if they repeat.

const correctValue = ['0', '1'];
const actualValue = [
    ['0', '2', '0', 'E', '1', '1', 'u', '0', '1', '0', ':', '1'],
    ['0', '0', '1', 'u', '0', '}', '1', 'l', '1', '1', '1', '˜'],
    ['x', 'k', 'e', '2', '|', '[', 'z', '8', 's', 'd', '2', '5'],
    ['r', '8', '6', ']', '2', '6', 'H', ';', 'Á', 'l', 'm', '?'],
    ['Y', '0', '0', '1', '1', '7', '1', '.', '0', 'v', '0', '1']
];
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
cpp
  • 1
  • 2
    Please provide what have you done till now?. Also provide sample input and expected output – Ashish Dec 20 '18 at 12:19
  • Do you mean intersection? Then your question might already be answered under https://stackoverflow.com/questions/1885557/simplest-code-for-array-intersection-in-javascript – schlingel Dec 20 '18 at 12:22
  • https://stackoverflow.com/a/37041756/4267015 duplicate – Naor Tedgi Dec 20 '18 at 12:23
  • I only have those two arrays. I would like to create a new array with all the "0" and "1" from the "actualValue" array. – cpp Dec 20 '18 at 12:25

4 Answers4

1

Try this Solution:-

   <!DOCTYPE html>
<html>
<body>




<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
const correctValue = ['0', '1'];
const actualValue = [
    ['0', '2', '0', 'E', '1', '1', 'u', '0', '1', '0', ':', '1'],
    ['0', '0', '1', 'u', '0', '}', '1', 'l', '1', '1', '1', '˜'],
    ['x', 'k', 'e', '2', '|', '[', 'z', '8', 's', 'd', '2', '5'],
    ['r', '8', '6', ']', '2', '6', 'H', ';', 'Á', 'l', 'm', '?'],
    ['Y', '0', '0', '1', '1', '7', '1', '.', '0', 'v', '0', '1']
];

function myFunction() {
var same_values=[];
for(var i=0;i<correctValue.length;i++)
{
    for(var j=0;j<actualValue.length;j++)
   {    
        for(var k =0;k<actualValue[j].length;k++)
           {
             if(correctValue[i]==actualValue[j][k])
              {
                same_values.push(actualValue[j][k])
              }
            }
    }
}
console.log(same_values)
}
</script>

</body>
</html>
Abhinav
  • 51
  • 5
0
function compareArrays(arr1, arr2) {
  return arr1.reduce((acc, item) {
    if (arr2.includes(item)) {
      acc.push(item)
    }
    return acc
  },[])
}

Not most efficient solution, but it works
Need to specify if you need only unique values or not
The best will be if you write expected output

0

You could use Array.prototype.filter(). Try this:

arr1.filter(e => arr2.includes(e));

Working demo:

 

function duplicate(arr1, arr2) {
   return arr1.filter(e => arr2.includes(e));
}

var duplicates = [];

const correctValue = ['0', '1'];
const actualValue = [
['0', '2', '0', 'E', '1', '1', 'u', '0', '1', '0', ':', '1'],
['0', '0', '1', 'u', '0', '}', '1', 'l', '1', '1', '1', '˜'],
['x', 'k', 'e', '2', '|', '[', 'z', '8', 's', 'd', '2', '5'],
['r', '8', '6', ']', '2', '6', 'H', ';', 'Á', 'l', 'm', '?'],
['Y', '0', '0', '1', '1', '7', '1', '.', '0', 'v', '0', '1']
];

actualValue.forEach(arr => duplicates.push(duplicate(correctValue, arr)))

console.log(duplicates);
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
  • It's not returning all the values. The output: [ [ '0', '1' ], [ '0', '1' ], [], [], [ '0', '1' ] ] – cpp Dec 20 '18 at 12:33
0

You can use reduce and filter to achieve what you are doing. In this approach, I am filtering each row in actualValue and concatenating the outputs.

const correctValue = ['0', '1'];
const actualValue = [
['0', '2', '0', 'E', '1', '1', 'u', '0', '1', '0', ':', '1'],
['0', '0', '1', 'u', '0', '}', '1', 'l', '1', '1', '1', '˜'],
['x', 'k', 'e', '2', '|', '[', 'z', '8', 's', 'd', '2', '5'],
['r', '8', '6', ']', '2', '6', 'H', ';', 'Á', 'l', 'm', '?'],
['Y', '0', '0', '1', '1', '7', '1', '.', '0', 'v', '0', '1']];

actualValue.reduce((arr, listData)=> arr.concat(listData.filter(data => correctValue.includes(data))),[])
console.log(output) //["0", "0", "1", "1", "0", "1", "0", "1", "0", "0", "1", "0", "1", "1", "1", "1", "0", "0", "1", "1", "1", "0", "0", "1"]
Ashish
  • 4,206
  • 16
  • 45