Eu tenho o seguinte JSON
{
"Id":"ssjsjs",
"Name":"STFksks S.A",
"Alias":"STF jnsns S.A",
"DocumentId":"010101",
"Accounts":[
"hahadkjkjteste"
],
"CountryCode":"BRA",
"Country":"Brasil",
"Address":{
"PostalCode":"222222",
"Street":"Largo dos Leões",
"State":"RJ",
"City":"Rio de Janeiro",
"Country":"Brazil",
"Number":"200",
"Complement":"",
"Neighborhood":"Humaitá",
"AddressType":"Comercial"
},
"TestContracts":[
{
"Name":"Teste nome",
"Email":"[email protected]",
"Phone":"55219999999",
"PhoneCountry":{
"DialCode":"55",
"Format":"+..-..-....-....",
"Iso":null
}
},
{
"Name":"hsshsh",
"Email":"teste2.teste",
"Phone":"5521981660129",
"PhoneCountry":{
"DialCode":"55",
"Format":"+..-..-....-....",
"Iso":null
}
},
{
"Name":"hahah",
"Email":"[email protected]",
"Phone":"552121212122",
"PhoneCountry":{
"DialCode":"55",
"Format":"+..-..-....-....",
"Iso":null
}
},
{
"Name":"Gus",
"Email":"[email protected]",
"Phone":"33333333",
"PhoneCountry":{
"DialCode":"55",
"Format":"+..-..-....-....",
"Iso":null
}
}
],
"PaymentType":0,
"PaymentStatusChangeDate":"2017-08-05T14:24:32.6503311Z",
"PaymentStatus":0,
"InscricaoMunicipal":"002",
"InscricaoEstadual":"",
"UnblockPaymentStatusRequest":"",
"WillBlockAt":"",
"Tier":"",
"ChargeStrategy":"Active",
"Address.PostalCode":"2226021"
}
Eu faço uma função para monitorar os dados que são modificados ao digitar algo no input, pegando o id do input que está sendo alterado, e modificando o valor para o novo valor digitado.
handleInputChange(event) {
const { Company } = this.state
Company[event.target.id] = event.target.value
this.setState({ Company: Company })
}
Mas quando tento acessar as propriedades do objeto Address
da seguinte maneira, recebo undefined:
handleInputChange(event) {
const { Company } = this.state
const targetId = event.target.id
if (targetId.includes('.')) {
const targetIdSplit = targetId.split('.')
const targetIdSplit0 = targetIdSplit[0]
const targetIdSplit1 = targetIdSplit[1]
console.log(Company[targetIdSplit0].targetIdSplit1)
}
}
Eu não consigo entender o porquê. Quem puder me mostrar a maneira certa de acessar esse objeto, eu agradeço muito.
[]
paratargetIdSplit1
também, para teres dois niveis de chaves dinamicas:Company[targetIdSplit0][targetIdSplit1]
. Se não fôr isso dá um exemplo deevent.target.id
.