循环数组和另一个对象中的对象

时间:2018-12-30 17:19:05

标签: javascript arrays reactjs loops object

我具有以下结构。我需要获得内部价值并在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"
      }
]

2 个答案:

答案 0 :(得分:4)

以下是使用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);

答案 1 :(得分:0)

这样做

import React from 'react'

export default class YourComponent extends React.Component {
    render() {
        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"
                }
            }
        ]

        let newArr = arr.map((data) => {
            return data.CoinInfo.FullName
        })
        console.log('new array', newArr);
        return (
            <div>
            </div>
        )
    }
}