【问题标题】:Loop object that in the array and in the another object循环对象在数组和另一个对象中
【发布时间】:2018-12-30 17:19:04
【问题描述】:

我有以下结构。我需要在 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"
      }
]

【问题讨论】:

    标签: javascript arrays reactjs loops object


    【解决方案1】:

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

    【讨论】:

    • 对不起@IonutAchim 我以为你在使用字符串时需要它们。将编辑
    • @JackBashford - 当您需要通过 var 查找密钥时需要它们。 const propName = 'Name'; const value = obj[propName];
    • letvar 更改为const,这个答案是完美的
    • 啊,当然@AmirPopovich 这很有道理——如果你在那个例子中使用点,它会创建一个字面上命名为 propName 的属性。感谢您澄清这一点。
    • 对象属性名称始终是字符串。仅当它们与保留字相同或不是有效的 J 标识符(例如,包含空格、连字符或以数字开头)时,才将它们用引号引起来。
    【解决方案2】:

    这样做

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

    【讨论】:

    • 因为这不是您使用map() 的方式,所以您的投票被否决了。 map() 创建并返回一个数组。使用它来循环并推入另一个数组是多余的。有关示例,请参见上面的答案。
    • @MarkMeyer 非常感谢您的帮助!我犯了错误,后来我改正了。
    • @MarkMeyer 我本可以做到的,但我认为这是我得到的最好的教训。
    猜你喜欢
    • 1970-01-01
    • 2020-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-22
    • 2012-04-05
    • 2021-09-07
    相关资源
    最近更新 更多