I have an 2 Array
var arr1 = ['A','B','C','D']
var arr2 = ['A~XX','A~XXX','B~YY','B~YYY']
I want push arr2 elements that contains ‘A’ to op_array
var op_array = new Array();
my op_array should look like this, If ‘A’ is passed this
['A~XX','A~XXX']
If ‘B’ is passed then
['B~YY','B~YYY']
I have been trying this
op_array = arr1.filter(item => !arr2.split(/~/)[1].includes(item));
Curious to learn an efficient way.
2
Answers
If you plan to do make lookup many times, say more than
log(n)
times, it makes sense to pre-create some sort of a set or dictionary for fast and efficient way.Try with below code