Accessing data from a js object

Asked

Viewed 1,945 times

1

I have the following JS object displayed by the console.log console.log

I can only access the status property of the object itself, but I need to access the status property that is within the value object and has the value 400, but all types of property advisors that I try to give Undefined when I try to go beyond the first status (value 0) presented. Can anyone help me? I would also like to know what is the name given to these "internal" properties of the object?

  • How did you try? By logging, it should be object.value.status. Maybe the problem is not the form of access, but the moment when you try to access this data (it may be that cestja trying to access before the data are available, if they come by Ajax)

  • To access is what the bfavarreto said, already with respect to the name your question is the answer, so much so that the method to return the name of the internal properties is Object.getOwnPropertyNames() are properties not?

  • Actually it is an ajax request made by a function Restangular.all, perhaps it is the question of the data not being available in time. I’m trying with object.value.status but I get Undefined or Tried to load Angularjs more than Once

  • Ask the question the code snippet that makes the request, and the snippet that tries to treat the answer.

1 answer

3


Like the bfavaretto said see the moment you are accessing the data as it may not yet be available, but the syntax is this as in the example. You have an object dice, that to access any of its properties just put the . before ownership. In your case there by image have no array otherwise you would have to access the property index to get its value:

var dados = {
  pure: true,
  status: 2,
  value: {
    data: "",
    status: 400,
    headers: "bla-bla-bla"
  }
}

console.log(dados.status +" - "+ dados.value.status );

  • 1

    Note the console output the question shows. It seems that the value of status era 0 at the time of the log, and 2 the moment he expanded the object. It is very expensive of the problem to be another: to try to access data of asynchronous operation before it is available.

  • Yes, true. Probably the problem was this same.

  • 1

    Guys, we chose to change the backend that the real cause of the problem that is occurring when casting an undue exception, the attempt on JS was more an attempt to solve in the gambit than to actually solve the problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.