根据包括列表在内的多个参数过滤 json 数组
filter json array based on multiple arguments including lists
所以我试图用这两个列表过滤 json 数组。我想要的是,如果这些列表中的任何元素在 json 数组中,我希望它们出现在输出中。
这是我的 json 数组。
[{
"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'],
}]
我有两个数组,一个是
listed_inilter = ['KSE100', 'KMIALLSHR']
sector_code = ['0833', '0824']
我想要的是从 json 数组输出数据,即使每个列表中的任何值都匹配。
你可以试试这个。研究 javascript 中的方法 filter 以了解以下代码的作用。
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
所以我试图用这两个列表过滤 json 数组。我想要的是,如果这些列表中的任何元素在 json 数组中,我希望它们出现在输出中。 这是我的 json 数组。
[{
"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'],
}]
我有两个数组,一个是
listed_inilter = ['KSE100', 'KMIALLSHR']
sector_code = ['0833', '0824']
我想要的是从 json 数组输出数据,即使每个列表中的任何值都匹配。
你可以试试这个。研究 javascript 中的方法 filter 以了解以下代码的作用。
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