I have this array of objects:
const data = [
{
id: 1,
name: 'Name1',
encryptionKey: 'AAA'
},
{
id: 2,
name: 'Name2',
encryptionKey: 'BBB'
},
{
id: 3,
name: 'Name3',
encryptionKey: 'CCC'
}
]
and another array of encryption keys:
const encryptionKeys = ['AAA', 'BBB']
I am then filtering the data array based on the encryptionKeys array like this:
var filtered = data.filter(function(item) {
return encryptionKeys.indexOf(item.encryptionKey) !== -1;
});
which works and filters the objects and saves them in a new array. The problem is however if the encryptionKey array has duplicated keys, for example:
const encryptionKeys = ['AAA', 'BBB', 'BBB']
then all duplicate keys will be ignored and the filtered array will only have, in this case, 2 objects instead of 3. What am I doing wrong in my filtering code? The filtered array should have duplicate objects if the encryptionKeys array has duplicate values.
encryptionKeyswhich are not indata?BBBexistsntimes then you want the Objects with the encryptionKeyBBBto appear in the filtered resultntimes?