Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How i can update or put a new object into array of object? For Beginners

Basically I have an array of objects, and I have created a new object that I want to examine, I have to see if the new object contains the same keys present in my array of objects, then see if it is a completely new object and then add it, or check if it already exists and if so update it, in this case if it is already present the request is to update only the ‘ingredients’ property

to be clearer this is the request
Okay this is the request : write a function that, given a recipe, adds it to the list of recipes or modifies the ingredients if it already exists.

I wanted to know if it is possible to do this using a type of map function, however I repeat I have just started and I get easily confused

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

my tutor told me that what I did is fine but it doesn’t give me any results when I go to view the array of objects

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",
  },
];

let oNuovo = {
  nome: "Tiramisu",
  ingredienti: ["uovo", "mascarpone", "savoiardo"],
  tipologia: "dolce",
};

function aggiungiRicetta(ricetta) {
  
  let cerca = (el) => ricetta.nome === el.nome;
  let trovato = ricette.findIndex(cerca); 
  if (trovato === -1) {
    ricette.push(ricetta);
  } else {
    ricette[trovato].ingredienti = ricetta.ingredienti;
    
  }
}

console.log(ricette)

>Solution :

Try out this approach. Use this only for learning purpose because you cannot modify or add new values in global object

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",
  },
];

let oNuovo = {
  nome: "Tiramisu",
  ingredienti: ["uovo", "mascarpone", "savoiardo"],
  tipologia: "dolce",
};

let existing = {
    nome: "Cicoria",
    ingredienti: ["aglio1", "peperoncino2"],
    tipologia: "contorno",
  };

function aggiungiRicetta(newReceipe) {
  let name = newReceipe.nome;
  let trovato = ricette.findIndex(rec=> rec.nome==name); 
  if(trovato === -1){
       ricette.push(newReceipe);
       return "New Receipe Added! "+name;
  }
  //Modify existing
  ricette[trovato].ingredienti = [...ricette[trovato].ingredienti,...newReceipe.ingredienti]
  return "Modified Successfully "+name 
}

console.log(aggiungiRicetta(oNuovo));

console.log(aggiungiRicetta(existing));

console.log(ricette);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading