I am facing one problem in javascript filter.
Suppose this is an array1-
const array1 = [
{
title: 'Stock market news',
symbols: ['SPY.US', 'GSPC.INDX', 'DJI.INDX', 'CL.COMM', 'IXIC.INDX', 'NQ.COMM', 'ES.COMM'],
},
{
title: 'Neil Young urges Spotify',
symbols: ['SPOT.US', '639.F', '639.XETRA']
},
{
title: 'Neil Young urges Spotify',
symbols: ['AAPl.US', '639.F', '639.XETRA']
}
]
And this is an array2
const array2 = [
{Code: "AAPL"},
{Code: 'SPOT'}
]
I have to filer array1 and remove an object that not complete the condition. The condition is if the array1 symbols contain at least one element of array2 Code. I mean if the array2 Code is match arry1 symbols field at least one element.
In the above example, the result should be-
const array1 = [
{
title: 'Neil Young urges Spotify',
symbols: ['SPOT.US', '639.F', '639.XETRA']
},
{
title: 'Neil Young urges Spotify',
symbols: ['AAPl.US', '639.F', '639.XETRA']
}
]
Because this two object contain AAPL and SPOT in symbols field. I think I can clear all the things.
I am trying in this way-
const filterData = array1.filter(function (array1El) {
return !array2.find(function (array2El) {
return array1El.symbols.includes(`${array2El.Code}.US`);
})
});
But it is not working. Please say me where I am wrong.