用美元符号引用对象

时间:2019-02-22 21:19:14

标签: javascript object

鉴于以下对象,如何引用email属性?我一直在尝试getresponse.partner [0] .email的变体,但无济于事。

[BUCKET NAME]: [BUCKET NAME]: the plan would destroy this resource,
 but it currently has lifecycle.prevent_destroy set to true. 
 To avoid this error and continue with the plan, either disable 
 lifecycle.prevent_destroy or adjust the scope of the plan using 
 the -target flag.

先谢谢! -b

2 个答案:

答案 0 :(得分:0)

$是Javascript中的有效属性/变量名称。就像使用其他任何属性名称一样使用它:

let $$$ = {
  "getresponse": {
    "$": {
      "unpagedCount": "10"
    },
    "partner": [{
      "$": {
        "id": "p1e",
        "type": "content",
        "name": "myname",
        "status": "active",
        "email": "me@here.com",
        "peopleIds": "9",
        "personIds": "9"
      },
      "ketchup": [""]
    }]
  }
}

console.log($$$.getresponse.partner[0].$.email);

答案 1 :(得分:0)

只需通过getresponse.partner[0].$.email在任何JS对象中将其作为普通键访问即可。请注意,$是合法的变量标识符名称,因为您可以检入其他post

var obj = {"getresponse":{"$":{"unpagedCount":"10"},"partner":[{"$":{"id":"p1e","type":"content","name":"myname","status":"active","email":"me@here.com","peopleIds":"9","personIds":"9"},"ketchup":[""]}]}}
var email = obj.getresponse.partner[0].$.email
console.log(email)