首页 > 解决方案 > 循环在数组和另一个对象中的对象

问题描述

我有以下结构。我需要在 React 中获得内部价值并通过。我想我需要获取一组值,例如: ['Bitcoin', 'Etherium'...] 并通过它进行映射。我该如何实施?

 let arr = [
      {
        "CoinInfo": {
                "Id": "1182",
                "Name": "BTC",
                "FullName": "Bitcoin",
                "Internal": "BTC",
                "ImageUrl": "/media/19633/btc.png",
                "Url": "/coins/btc/overview"
            }
      },
      {
         "CoinInfo": {
            "Id": "7605",
            "Name": "ETH",
            "FullName": "Ethereum",
            "Internal": "ETH",
            "ImageUrl": "/media/20646/eth_logo.png",
            "Url": "/coins/eth/overview"
      }
]

标签: javascriptarraysreactjsloopsobject

解决方案


这是您如何使用获得一组硬币名称的方法Array.prototype.map()

const arr = [{
    "CoinInfo": {
      "Id": "1182",
      "Name": "BTC",
      "FullName": "Bitcoin",
      "Internal": "BTC",
      "ImageUrl": "/media/19633/btc.png",
      "Url": "/coins/btc/overview"
    }
  },
  {
    "CoinInfo": {
      "Id": "7605",
      "Name": "ETH",
      "FullName": "Ethereum",
      "Internal": "ETH",
      "ImageUrl": "/media/20646/eth_logo.png",
      "Url": "/coins/eth/overview"
    }
  }
];

const coinNames = arr.map(x => x.CoinInfo.FullName);

console.log(coinNames);


推荐阅读