括号符号+ JavaScript [英] bracket notation + javascript

查看:72
本文介绍了括号符号+ JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的对象:

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

那么:什么是使用刹车片的正确形式? 上下文分别在一个jquery中:

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?

推荐答案

我相信你是在说你的对象是:

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

Customer["country"]["city"]

或这两者的任意组合

还请注意,Customer[country[something]]也可以是有效的语法,但对于您而言似乎并非如此

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'] 

还将返回您的城市Peru

这篇关于括号符号+ JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆