I have data which is structured like so:
[
{title: 'a', washday: 'true', cutday: 'false', colorday: 'true', deepconditionday: 'true'},
{title: 'b', washday: 'false', cutday: 'true', colorday: 'false', deepconditionday: 'false'},
{title: 'c', washday: 'true', cutday: 'false', colorday: 'true', deepconditionday: 'true'},
{title: 'd', washday: 'false', cutday: 'false', colorday: 'false', deepconditionday: 'false'},
]
I also have states in my app for cutday , colorday , deepconditionday and washday where each one has a switch to toggle between booleans true and false.
How can I filter the above data according to which states are switched on? For example if only colorday is switch on, I want to show all items where colorday is 'true'. If both deepconditionday and cutday are switched on, I want to show all items where both cutday and deepconditionday are 'true'.
Thanks.
maybe like so:
let titleFilter = "a";
let washdayFilter = true;
let cutdayFilter = true;
let filteredData = data.filter(
(datapoint) =>
datapoint.title == titleFilter &&
datapoint.washday == washdayFilter.toString() &&
datapoint.cutday == cutdayFilter.toString()
);
const data = [
{title: 'a', washday: 'true', cutday: 'false', colorday: 'true', deepconditionday: 'true'},
{title: 'b', washday: 'false', cutday: 'true', colorday: 'false', deepconditionday: 'false'},
{title: 'c', washday: 'true', cutday: 'false', colorday: 'true', deepconditionday: 'true'},
{title: 'd', washday: 'false', cutday: 'false', colorday: 'false', deepconditionday: 'false'}
]
const filterData = (data, isWashDay, isCutDay, isColorDay, isDeepConditionDay)=>{
return data.filter((item)=>{
(isWashDay === 'any' | isWashDay ==== item.washday) &&
(isCutDay === 'any' | isCutDay ==== item.cutday) &&
(isColorDay === 'any' | isColorDay ==== item.colorday) &&
(isDeepConditionDay === 'any' | isDeepConditionDay ==== item.deepconditionday)
})
}
console.log(filterData(data, 'true', 'any', 'any', 'any'))
More info here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
Do take note that you've used 'true' and 'false' as strings, and not booleans, so I did the same. You should use a boolean and change the code accordingly though https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean
I have implemented example use ReactJS and MaterialUI. Just use states for switchs and filter by states. Link demo: https://codesandbox.io/s/material-demo-forked-176e3?file=/demo.js
import React, { useState } from 'react';
import Switch from "@material-ui/core/Switch";
export default function DisableElevation() {
const [isWashday, setIsWashday] = useState(false);
const [isCutday, setIsCutday] = useState(false);
const [isColorday, setIsColorday] = useState(false);
const [isDeepconditionday, setIsDeepconditionday] = useState(false);
const data = [
{title: 'a', washday: true, cutday: false, colorday: true, deepconditionday: true},
{title: 'b', washday: false, cutday: true, colorday: false, deepconditionday: false},
{title: 'c', washday: true, cutday: false, colorday: true, deepconditionday: true},
{title: 'd', washday: false, cutday: false, colorday: false, deepconditionday: false},
];
const handleChange = (event) => {
switch (event.target.name) {
case 'isWashday':
setIsWashday(event.target.checked);
break;
case 'isCutday':
setIsCutday(event.target.checked);
break;
case 'isColorday':
setIsColorday(event.target.checked);
break;
case 'isDeepconditionday':
setIsDeepconditionday(event.target.checked);
break;
default:
}
};
return (
<div>
<div> {data.filter(datum => (!isWashday || datum.washday) && (!isCutday || datum.cutday) && (!isColorday || datum.colorday) && (!isDeepconditionday || datum.deepconditionday)).map(r => r.title + " ")} </div>
<div>
<span>isWashday</span>
<Switch
checked={isWashday}
onChange={handleChange}
name="isWashday"
inputProps={{ "aria-label": "secondary checkbox" }}
/>
</div>
<div>
<span>isCutday</span>
<Switch
checked={isCutday}
onChange={handleChange}
name="isCutday"
inputProps={{ "aria-label": "secondary checkbox" }}
/>
</div>
<div>
<span>isColorday</span>
<Switch
checked={isColorday}
onChange={handleChange}
name="isColorday"
inputProps={{ "aria-label": "secondary checkbox" }}
/>
</div>
<div>
<span>isDeepconditionday</span>
<Switch
checked={isDeepconditionday}
onChange={handleChange}
name="isDeepconditionday"
inputProps={{ "aria-label": "secondary checkbox" }}
/>
</div>
</div>
);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.