In a React project, I want to club certain records into an empty array, to pass into some function. So, on check of checkbox component I'm able to get the id of the record which I want to compare it with the array of data. My concern is to push all the checked records to newly created empty array, to pass it to some function. I have gone through various posts but, didn't get any satisfactory result. Please refer to the following code.
const newAllData = [{testId:123, dataId:"44", cycleId:"5050", asset:"N"},
{testId:323, dataId:"54", cycleId:"6660", asset:"N"},
{testId:444, dataId:"44", cycleId:"4340", asset:"Y"},
{testId:134, dataId:"40", cycleId:"5150", asset:"N"},
{testId:222, dataId:"42", cycleId:"5050", asset:"Y"},
{testId:552, dataId:"38", cycleId:"3244", asset:"Y"},
]
const [newArray, setNewArray] = useState([])
// Here I get id of the reocord checked
const getArray = (id) => {
//Here I find that particular record from data above
const newObj = newAllData?.find(d => d.testId == id);
var arrayData = [];
arrayData.push(...newObj);
setNewArray(arrayData)
}
console.log("NEW ARRAY", newArray)
return <>
<CheckboxComp getArrayData={getArray} />
</>;
Here only one record is populating in the newArray. What is the right approach to get all the records.