in a situation I need to update a person's number attribute, before update I need to generate a window confirm if that person's name is find already in db.json. How do you think I can do this ? To be more specific:
<form onSubmit={addName}> needs to do things depends on what I have in db.json. if the name exists, then addName does update, if not addName adds new input. the current code always add half duplicate data input i.e., different id, number, but same name. And when deleting data gives map not a function
const addName = (event) => {
event.preventDefault()
const nameObj = {
name: newName,
number: newNumber,
id: nanoid()
}
if (persons.find((person)=> person.name === newName)){
//myArray.find(x => x.id === '45').foo;
const id = persons.find(person=>person.name===newName).id
const changedPerson = {...nameObj, number: nameObj.number}
if (window.confirm(`${newName} already there`)) {
personService
.update(id, changedPerson)
.then(returnPerson=>{
setPersons(persons.map(person => person.id === id ? person : returnPerson))
setNewName('')
setNewNumber('')
return
})
}
}
personService
.create(nameObj)
.then(returnPerson=>{
setPersons(returnPerson)
setNewName('')
setNewNumber('')
})
}