I want add data to array of object on button click, if index value not present create new object and add data
const [sampleArray, setSampleArray] = useState([{fruit:'', Vegetable:''}])
const addData=(index, value)=>
{
setSampleArray()
}
on first click : index = 1 & fruit = 'orange'
on second click : index = 1 & vegetable = 'brinjal'
on third click : index = 2 & fruit = 'Banana'
on fourth click : index = 2 & fruit = 'cabbage'
<button onClick={addData}> submit </button>
result should be like this:
[{fruit:'orange', Vegetable:'brinjal'}, {fruit:'Banana', Vegetable:'cabbage'}]
addDatamethod looks incomplete.setSampleArray().