-1
I wonder how I can put the contents of an array that is with true status within another array, for example: { id:1, Fruit:'Banana', status:true }, { id:2, Fruit:'Orange', status:true }, { id:3, Fruit:'Lemon', status:true }
I tried to do according to the code below, but it is putting the item as an array inside the other and not only the content:
const [fruits,setFruits] = useState([
{
id:1,
fruit:'Banana',
status:true,
},
{
id:2,
fruit:'Orange',
status:true
}
]);
const [newFruit, setNewFruit] = useState([
{
id:3,
fruit:'lemon',
status:true
},
{
id:4,
fruit:'morango',
status:false
}
]);
JSX code below:
return (
<div>
{fruits.map((fruits,index)=>
<span key={index}>{fruits.fruit}, </span>
)
}
<button onClick={Add}>Adicionar</button>
</div>
);
Function to add new fruit:
const Add = () =>{
const filtered = newFruit.map((fruit)=>
fruit.status === true
? {
fruit
}
: {
}
)
setFruits([
...fruits,
filtered
])
console.log(fruits)
}
Opa, really got as I expected, did not know that I could destroy the tb Filtered. Thank you very much!
– Gabriel Mariano