3
Suppose I have the following object:
var pessoa = {
nome: "João",
animais: {
cachorro: "Rex",
gato: "Pipoca",
}
}
I need a function to do something like this:
var gatoDoJoao = pessoa["animais.gato"];
I know that in this example the correct ispessoa.animais.gato
, but I want the value to be accessed through a string.
Something like: var gatoDoJoao = acessar(pessoa, "animais.gato");
I know this method too, but I’m looking for a way to access the string in this "animals.gato" format, because this string comes from a text field. , as mentioned, a function that receives the object and the string. access(person, "animals.cat");
– Salomão Neto
Then the question is another, you would have to analyze the string, separate it and use it the way I presented it. Your question does not clearly state that it needs to be so, it does not say what criteria, what to do when invalid text comes along, etc. So if the question is by this way it is unclear and is ample by asking for a complete solution. You could even ask a new more detailed question, but you would have to give all the necessary details and try to do it. Without criterion I would say that it is enough to make one
split(".")
, but it’s a pretty naive way to solve this.– Maniero