1

This is my object :

Customer{"id": "0001", "name": "ivan" , "country" {"city" : "Peru"}}

So: Whats the correct form to use brakets? the context is in a jquery each:

$. each (datos, function (index, data) { }

1° data["country"["city"]]   >> the result should be "Peru"
2° data["country"]["city"]   >> the result should be "Peru"

or whats is the form correct?

5
  • 3
    None of this is really valid ? Commented Mar 28, 2014 at 18:16
  • 1
    Note : Object == {} && Array == []. Use a proper syntax. Commented Mar 28, 2014 at 18:17
  • The syntax is incorrect. Brackets [] are used to retrieve a value from an array or object at a particular index. The index is the value within the brackets. Commented Mar 28, 2014 at 18:19
  • Also, anything like this can never exist: Customer[id: "0001"; name: "ivan" ; country [city : "Peru"]] Commented Mar 28, 2014 at 18:20
  • I edit my post, I feel I have been confused Commented Mar 28, 2014 at 18:31

2 Answers 2

2

I belive you are saying that your object is :

Customer  = {
   id: "0001",
   name: "ivan",
   country: {
      city : "Peru"
   }
}

In this case your syntax would be either

Customer.country.city

or

Customer["country"]["city"]

or any mix of this two

Note also that Customer[country[something]] can also be valid syntax, but doesnt seem to be in your case

Customer  = {
   id: "0001",
   name: "ivan",
   country: {
      city : "Peru"
   }
}


country = {
   key: 'country'
}

Customer[country['key']]['city'] 

would also return you city Peru

Sign up to request clarification or add additional context in comments.

1 Comment

Tnx for answer and correct me. I tried that way but something is wrong, I get null Customer["country"]["city"]
1

This is not an JavaScript object:

Customer[id: "0001"; name: "ivan" ; country [city : "Peru"]]

This is an object in JavaScript object:

var customer = {
   id: "0001",
   name: "ivan",
   country: {
      finland: {
         city: "Helsinki"
      }
   }
};

You would use it with this:

console.log(customer.country.finland.city);

or this:

console.log(customer['country']['finland']['city']);

or with mixing....

console.log(customer['country'].finland.city);

..and yes took a freedom of adding actual country, not just city but I guess this post demonstrates the point, you can either retrieve values with dot notation customer.country.finland or apostrophes customer['country']['finland'] .. However, if you are using numbers as JavaScript object key like this:

var customer = {
  1: "Mauno"
};

You can retrieve it only using apostrophes as: customer['1'] trying to use it like customer.1 will result in a JavaScript error.

1 Comment

np. glad that you got it solved. Hopefully my post still helps future readers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.