I’m a beginner in JavaScript and today I have a question:
- How can I update or put a new object into an array of object?
- I need to check if the new object is totally new and then I push the new object into the array of object
- If the new object have the properties
'ingredienti'
that are the same as an existing object in the array and if they’re different I will need to update the array of object.
Because I know that I will use push and everything it’s okay, but I will check if the new object is totally new and then I push the new object into the array of object, and if the new object have the properties 'ingredienti'
are the same of the object into array and if they’re different I will update the array of object. So idk what I can do, I tried everything but the only result is a new object into the array 'ricette'
var ricette = [
{
nome: "Carbonara",
ingredienti: ["guanciale", "uovo", "sale"],
tipologia: "primo",
},
{
nome: "Amatriciana",
ingredienti: ["guanciale", "pecorino", "pomodoro"],
tipologia: "primo",
},
{
nome: "Cacio e Pepe",
ingredienti: ["pepe", "pecorino"],
tipologia: "primo",
},
{
nome: "Cicoria",
ingredienti: ["aglio", "peperoncino"],
tipologia: "contorno",
},
{
nome: "Tiramisu",
ingredienti: ["uovo", "mascarpone", "savoiardo"],
tipologia: "dolce",
},
];
let nuovo = ricette.push({
nome: "Tofu panato",
ingredienti: ["farina", "pangrattato", "olio"],
tipologia: "secondo",
});
2
Answers
You can use a loop to iterate through the array and check if a matching object exists or not.