0

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

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

6 Comments

Sorry @IonutAchim I thought you needed them when using strings. Will edit
@JackBashford - You need them when you need to lookup a key via a var. const propName = 'Name'; const value = obj[propName];
change let and var to const and this answer is perfect
Ah, of course @AmirPopovich That makes sense - if you used dots in that example, it would create a property literally named propName. Thanks for clarifying that.
Object property names are always strings. You only wrap them in quotes when they are the same as reserved words or are not valid J's identifiers (e.g. contain a space, hyphen or starts with a number).
|
0

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

3 Comments

You’re getting downvotes because this is not how you use map(). map() creates and returns an array. Using it to loop over and push into another array is redundant. See the answers above for examples.
@MarkMeyer Thank you so much for the help! I did the mistake and later I have corrected it.
@MarkMeyer I could have done it but I thought this is the best lesson I got.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.