简体   繁体   English

如何访问 javascript 中的 object 中的值?

[英]How can I access a value in an object in javascript?

I have an object like this which I console log like this:我有一个像这样的 object,我这样控制日志:

console.log(node.data.target.fields);
file:
en-US: {url: "//images.ctfassets.net/qkwv5aiilkmk/4YsaPFrSxMPKuMVnO/efb6b59a369e4f30105aaea54fb9f62f/aaa.jpeg", details: {…}, fileName: "aaa.jpeg", contentType: "image/jpeg"}

I want to access the url value but It throws an error:我想访问 url 值,但它会引发错误:

const url = node.data.target.fields.file.['en-US'].url;

How can I access the en-Us variable properly?如何正确访问 en-Us 变量?

Since en-US cannot be used as a variable name ( - is an operator, cannot be used as a part of a variable), you cannot use the dot notation.由于en-US不能用作变量名( -是运算符,不能用作变量的一部分),因此不能使用点符号。 So the only way you can access is using the [] notation.因此,您可以访问的唯一方法是使用[]表示法。

const url = node.data.target.fields.file['en-US'].url;

The above is the right way of accessing it, else, you can also access this way:以上是正确的访问方式,否则你也可以这样访问:

const lang = 'en-US';
const url = node.data.target.fields.file[lang].url;

Two cases example:两种情况示例:

 const node = { data: { target: { fields: { file: { "en-US": { url: "//images.ctfassets.net/qkwv5aiilkmk/" } } } } } }; (() => { console.log("Using direct access..."); const url = node.data.target.fields.file['en-US'].url; console.log(url); })(); (() => { console.log("Using a variable..."); const lang = 'en-US'; const url = node.data.target.fields.file[lang].url; console.log(url); })();

To add from the given answer just a few guidelines for accessing Object Properties here are some tips that would help you in the future.要从给定的答案中添加一些访问 Object 属性的指南,这里有一些技巧可以在将来帮助您。

1. Dot property accessor 1.点属性访问器

This common way to access the property of an object is the dot property accessor syntax: expression.identifier这种访问 object 属性的常用方法是点属性访问器语法: expression.identifier

const hero = {
  name: 'Batman'
};

// Dot property accessor
hero.name; // => 'Batman'

1.1 Dot property accessor requires identifiers 1.1 点属性访问器需要标识符

-The dot property accessor works correctly when the property name is a valid identifier. - 当属性名称是有效标识符时,点属性访问器可以正常工作。 An identifier in JavaScript contains Unicode letters, $, _, and digits 0..9, but cannot start with a digit. JavaScript中的标识符包含Unicode个字母、$、_和数字0..9,但不能以数字开头。

const weirdObject = {
  'prop-3': 'three',
  '3': 'three'
};

weirdObject.prop-3; // => NaN
weirdObject.3;     // throws SyntaxError: Unexpected number
  • To access the properties with these special names, use the square brackets property accessor (which is described in the next section):要访问具有这些特殊名称的属性,请使用方括号属性访问器(将在下一节中介绍):
const weirdObject = {
  'prop-3': 'three',
  '3': 'three'
};

weirdObject['prop-3']; // => 'three'
weirdObject[3];        // => 'three'

2. Square brackets property accessor: 2.方括号属性访问器:

-expression[expression] -表达式[表达式]

const property = 'name';
const hero = {
  name: 'Batman'
};

// Square brackets property accessor:
hero['name'];   // => 'Batman'
hero[property]; // => 'Batman'

3. Object destructuring: 3. Object解构:

  • const { identifier } = expression; const { 标识符 } = 表达式;
const hero = {
  name: 'Batman'
};

// Object destructuring:
const { name } = hero;name; // => 'Batman'

Note that you can extract as many properties as you'd like: const { identifier1, identifier2, .., identifierN } = expression;请注意,您可以提取任意数量的属性: const { identifier1, identifier2, .., identifierN } = expression;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM