1
Tudo Bom?
I am trying to change an existing object by inserting new values in it.
Ex:
// Tenho um Objeto Values
let values = {
customer: {
name: "Fulana",
address: {
street: "Rua D"
}
}
}
I need a function where I can pass this object, a path and a new value per parameter. The path is the place where I want to store my new value (Ex: ["Customer" "address", "number"])
At the moment I only have the function that will generate an object for me, but I do not know how to change this object without losing the existing values
//função para criar objeto
changeObject(path, value){ // path = ["customer", "address", "number"] value = "34"
let curr = value;
for(let i=path.length-1; i >= 0; i--){
let o = {};
o[path[i]] = curr;
curr = o;
}
return curr;
}
//Depois de passar pela função quero que retorne algo assim
values = {
customer: {
name: "Fulana",
address: {
street: "Rua D",
number: 34,
}
}
}
Currently I only have the function that generates the object
Vlw Sergio, Tu é brabo. Tmj ;)
– Deiveson