So I am trying to filter a json array with these two lists. what I want is if any of the element from these lists are in json array I want them in the output. this is my json array.
[{
"sector_code": "0828", "listed_in": ["ALLSHR,KMIALLSHR" ],
},
{
"sector_code": "0828","listed_in": [ "KMI30"],
},
{
"sector_code": "0824","listed_in": ["KSE100,ALLSHR,KMIALLSHR"],
},
{
"sector_code": "0833","listed_in": [ "KSE100", 'KMIALLSHR', 'LST30'],
}]
And I have two arrays one is
listed_inilter = ['KSE100', 'KMIALLSHR']
sector_code = ['0833', '0824']
What I want is output the data from the json array even if any of the value from each list is matching.
You can try this on. Study about method filter in javascript to know what the code below does.
const array = [{ sector_code: ..., listed_in: ... }]; // your array
const listed_inilter = ['KSE100', 'KMIALLSHR'];
const sector_code = ['0833', '0824'];
function array_subset() {
return array.filter(element => {
const is_listed = listed_inilter.some(item => element.listed_in.includes(item));
const is_sector = sector_code.includes(element.sector_code);
return (is_listed || is_sector)
});
}
console.log(array_subset()); // output the filtered list