0

I have 1 input:

 <input
     type="text"
     name={item.job_n}
     defaultValue={item.azienda}
     readOnly={isReadonly}
     onChange={handleChange}                                                             
                                   

And onChange I'm adding the inputs value and name as an Object to an array of Objects in useState=([]), therefore my array looks like this: [{azienda:'', job_n:''}, {azienda:'', job_n:''}]

const [azienda, setAzienda] = useState([]);
    const handleChange =(e)=>{
        {
            const azienda_name = e.target.value
            const job_n = e.target.name            
            setAzienda((prevState) => [...prevState, {azienda: azienda_name, job_n: job_n}])                                    
        }   
    }

The code works fine and a new Object is added to my array each time an onchange event occurs. The issue I'm having is that if an object with job_n already exists in the array I'd need to update that object and not create a new object for each onchange.

3 Answers 3

2

I have changed the variables from italian to english so that everyone can understand them, and also changed some variables to follow the javascript naming conventions.

function handleChange(e) {
    const businessName = e.target.value;
    const jobName = e.target.name;
    const jobIndex = business.findIndex(v => {
        return v["jobName"] === jobName;
    });
    if (jobIndex === -1) {
        setBusiness([...business, { businessName, jobName }]);
        return;
    }
    const businessClone = [...business];
    businessClone[jobIndex] = { businessName, jobName };
    setBusiness(businessClone);
}
Sign up to request clarification or add additional context in comments.

2 Comments

This seems to work, grazie!
@zacaaron np, flag it as the answer.
0

Change your code to,

setAzienda((prevState) => {
      const jobExistIndex = prevState.findIndex(obj => obj.job_n === job_n)
      const jobExist = prevState[jobExistIndex]
      if (jobExist) {
          const newState = [...prevState]
          newState[jobExistIndex] = {...jobExist, job_n: job_n}
          return newState 
      }
      else {
          const newState = [...prevState]
          return [...newState, {azienda: azienda_name, job_n: job_n}]
      }

1 Comment

Still doesn't work. Need to change azienda in found value, not job_n
0
const handleChange = e =>{
{
    const azienda_name = e.target.value;
    const job_n = e.target.name;
    const existIndex = azienda.findIndex(p => p.job_n === job_n);
    if(existIndex != -1)
    {
       azienda[existIndex].azienda_name = azienda_name 
    }
    else
    {
       azienda.push({azienda_name : azienda_name, job_n: job_n})
    }        
    setAzienda(azienda);
}                                   
        

10 Comments

that would be invalid, as you are editing a const variable.
@Scar the variable is object, if it is a primitive type, throw errors.
I don't get what you mean but const [azienda, setAzienda] = useState([]); defines azienda as a constant, and therefore if u try to change it like u did after you first if condition it will throw.
@Codebling I don't know, but i've tried it in runtime and it gives out error.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.