Had an Array, have an update Array function, input will be taken through Form. Upon Input of ID,I want to check if the ID already exist in the array
Have given input as 1 ,which should give true.
It is showing "false" in console.
const bioData = [
{id:1}
]
const [myArray, setmyArray] = useState(bioData);
const [sid, setId] = useState("");
const handleID = (e) => {
setId(e.target.value);
}
const updateArray = () =>{
const isFound = myArray.some(el => el.id === sid);
console.log(isFound);
if (isFound) {
console.log('✅ array contains object with id = 1');
}
}
<input type="number" placeholder='Please enter your ID' className='inputelm' onChange={handleID} />
<button className='addbtn btn inputelm' onClick={updateArray}>Add</button>