我正在尝试过滤一个数组,以便对象与filterOptions
(另一个数组)中的选项之一匹配时显示。
示例:如果filterOptions
包含字符串big
,则应显示带有sizes
且包含字符串big
的对象
但是,我不确定如何正确分割data.sizes
,以便它可以使用此数据进行过滤,我该怎么做?
https://codesandbox.io/s/thirsty-ellis-dplhg
export const App = () => {
const data = [
{sizes: 'big,small',},
{sizes: 'medium,small'},
{sizes: 'small,big',},
{sizes: 'big',},
{sizes: 'medium',}
];
const filterOptions = ['big', 'small'];
const splitData = data.map(items => items.sizes.split(','));
return(
<div>
{console.log(splitData)}
{data.filter(items => items.sizes.includes(filterOptions))
.map(item => <div>{item.sizes}<br /></div>)}
</div>
)
}
答案 0 :(得分:1)
此答案可以容纳尽可能多的过滤器选项
data.filter(items => items.sizes.split(',').find(size => filterOptions.includes(size)))
答案 1 :(得分:1)
我相信您正在寻找:
{data.filter(i => i.sizes.split(",").some(size => filterOptions.includes(size)))
.map(item => <div>{item.sizes}<br/></div>)}
const data = [
{sizes: 'big,small'},
{sizes: 'medium,small'},
{sizes: 'small,big'},
{sizes: 'big'},
{sizes: 'medium'}
];
const filterOptions = ['big','small'];
let res = data.filter(
i => i.sizes.split(",").some(
size => filterOptions.includes(size)
)
);
console.log(res);
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}
但是,对于您的特定大小集("small"
,"big"
和"medium"
),如果没有字符串也是另一个字符串的子字符串,则可以使用下一个使用从filterOptions
数组构造的正则表达式进行过滤:
const data = [
{sizes: 'big,small'},
{sizes: 'medium,small'},
{sizes: 'small,big'},
{sizes: 'big'},
{sizes: 'medium'}
];
const filterOptions = ['big','small'];
let res = data.filter(
({sizes}) => new RegExp(filterOptions.join("|")).test(sizes)
);
console.log(res);
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}
或者,在您的代码上下文中:
{data.filter(({sizes}) => new RegExp(filterOptions.join("|")).test(sizes))
.map(item => <div>{item.sizes}<br/></div>)}
答案 2 :(得分:0)
这是您可能会做的事情(尽管有很多方法可以实现您的结果):
const data = [
{ sizes: 'big,small', },
{ sizes: 'medium,small' },
{ sizes: 'small,big', },
{ sizes: 'big', },
{ sizes: 'medium', }
];
const filterOptions = [ "big", "small" ];
const filterData = data.filter( ( { sizes } ) => {
for ( const filter of filterOptions ) {
if ( sizes.includes( filter ) ) {
return true;
}
}
} );
console.log( filterData );
这当然是假设您不希望数据大小都包含两者 filterOptions
。
答案 3 :(得分:0)
您在寻找
DELIMITER $$
CREATE TRIGGER `ISI_YUKSEK_ISE` BEFORE INSERT ON `TB1` FOR EACH ROW BEGIN
DECLARE
D_ISI INT ;
SET
D_ISI = NEW.D_ISI ; IF
(SELECT D_ISI = NEW.D_ISI FROM TB1,TB2 WHERE TB2.CIHAZ_ID=TB1.CIHAZ_KODU AND TB1.D_ISI >= TB2.MAX_ISI OR TB1.D_ISI <= TB2.MIN_ISI) THEN
INSERT
INTO
TB3(CIHAZ_KODU,D_ISI)
VALUES(NEW.CIHAZ_KODU,NEW.D_ISI) ;
END IF ; END
$$
DELIMITER ;