Grab list of objects within the array

Asked

Viewed 44 times

0

I’m having some difficulty to get the values that are of my function and pass them to a list, follow the code:

const [listaPais, setListaPais] = useState<{ id: number; name: string }[]>(
    []
);
useEffect(() => {
    setListaPais([
        { id: 1, name: "pais" },
        { id: 2, name: "pais 2" },
    ]);
}, []);

{listaPais.map((_item, index) => (
   <MenuItem key={index} value={''}>
        {}
   </MenuItem>
))}
  • _item.name and _item.id? Since the object has a id, avoid using the index as key

2 answers

1


You can do it like this:

{listaPais.map((item) => (
   <MenuItem key={item.id}>
        <span>{item.name}</span>
   </MenuItem>
))}

0

{
planos.map(function (plano, index) {

    return (
        <div key={index} className="form-row">
            <div className="form-group col-6 col-sm-6 col-md-8 col-lg-4">
                <input type="text" className="form-control" value={plano.nome} readOnly={true} />
            </div>

            <div className="form-group col-4 col-sm-6 col-md-4 col-lg-4" >
                <Money type="text" className="form-control" value={plano.valor} id={index} onChange={(e) => { onChangeValuePlano(e, index) }} />
            </div>
        </div>
    );
})

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.