i want to return a object to my array but it return number values in arry
const mappingFunction = (values: any) => {
values.forEach((reason: any) => {
const selectedReasons: any = [];
if (reason?.isChecked === "false") {
selectedReasons?.push(reason);
}
});
setSelectedReasons(values);
};
2
Answers
Declare the variable before the loop, not for every item:
You define
selectedReasons
inside your loop which will create a new instance on every iteration. You also passsetSelectedReasons
to the values that the function takes in.You could use the
filter
method to get only the values where thereason
isfalse
.