Sasha Zoria
Sasha Zoria

Reputation: 771

Loop object that in the array and in the another object

i have the following structure. I need to get Internal value and through in the React. I think i need to get an array of values, for example: ['Bitcoin', 'Etherium'...] and map through it. How can i implement it?

 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"
      }
]

Upvotes: 0

Views: 80

Answers (2)

Jack Bashford
Jack Bashford

Reputation: 44145

Here's how you'd get an array of coin names using 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);

Upvotes: 4

Vikas Singh
Vikas Singh

Reputation: 1857

Do it like this

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>
        )
    }
}

Upvotes: 0

Related Questions