JavaScript 嵌套对象数据访问

发布于 2025-01-14 19:06:03 字数 404 浏览 1 评论 0原文

我用来访问数据的代码在此处输入图像描述该图片包含来自国家/地区休息 API 的国家/地区数据 货币: 美元: 名称:“美元” 符号:“$”

在这种情况下,关键字 USD 对于每个国家/地区都是不同的,在这种情况下如何通过currency 属性访问该 USD 及其内部属性(名称、符号)。 让currency = { usd: { name: ' ', symbol: ' ' } }; 我用这一行来声明变量,但这一行将更新所有国家的美元。

code I'm using to access dataenter image description herethe picture includes country data from country rest API
currencies:
USD:
name: "United States dollar"
symbol: "$"

In this situation, the keyword USD is different for each country, in that case how to access that USD and properties inside it(name, symbol) through currencies property.
let currencies = { usd: { name: ' ', symbol: ' ' } };
I used this line to declare variable but this line will update usd for all the countries.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暖阳 2025-01-21 19:06:03

我们没有得到完整的示例,但我假设您有类似的示例。这可能解释了如何访问这些值。

const data = [
  {
    country: "United States",
    currencies: {
      usd: {
        name: "US Dollar",
        symbol: "$",
      },
    },
  },
  {
    country: "England",
    currencies: {
      usd: {
        name: "Sterling Pound",
        symbol: "£",
      },
    },
  },
  {
    country: "Germany",
    currencies: {
      usd: {
        name: "Euro",
        symbol: "€",
      },
    },
  },
];

首先,您需要获取您要尝试解构的财产,假设您要访问的国家是美国。

const selectedCountryData = data.find((val) => val.country === "United States");

然后通过解构简单地提取值

const { currencies: { usd:{ name, symbol } } } = selectedCountryData;

We didn't get a full example but I assume you have something like this. This might explain how to access these values

const data = [
  {
    country: "United States",
    currencies: {
      usd: {
        name: "US Dollar",
        symbol: "
quot;,
      },
    },
  },
  {
    country: "England",
    currencies: {
      usd: {
        name: "Sterling Pound",
        symbol: "£",
      },
    },
  },
  {
    country: "Germany",
    currencies: {
      usd: {
        name: "Euro",
        symbol: "€",
      },
    },
  },
];

First you need to get property you're trying to destructure, let's say the country you're trying to access is the US.

const selectedCountryData = data.find((val) => val.country === "United States");

And then simply extract values by destructuring

const { currencies: { usd:{ name, symbol } } } = selectedCountryData;
走走停停 2025-01-21 19:06:03

兄弟,如果您

data={
currencies:{ 
    usd: { name: 'a', symbol: 'b' }
},

}

需要将 Objtect 转换为数组

if need key use Object.keys(data.currencies )   //get usd 
if need value use Object.values(data.currencies )  //get [name:'a' , symbol:'b']
and if need all  use Object.entries(data.currencies);

//get 
[                                       
0:[usd]    
1:[name:'a' , symbol:'b']
]

,那么
如果你想要名称的值,你可以使用

Object.value(data.currencies)[0].name;       //get a 

bro if you have

data={
currencies:{ 
    usd: { name: 'a', symbol: 'b' }
},

}

need convert Objtect to an array

if need key use Object.keys(data.currencies )   //get usd 
if need value use Object.values(data.currencies )  //get [name:'a' , symbol:'b']
and if need all  use Object.entries(data.currencies);

//get 
[                                       
0:[usd]    
1:[name:'a' , symbol:'b']
]

then
if you want the value of name you can use

Object.value(data.currencies)[0].name;       //get a 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文