简体   繁体   English

如何使用 JavaScript 访问嵌套 graphql 响应的不同级别?

[英]How to access different levels of a nested graphql response using JavaScript?

I have a graphql response that looks like this:我有一个看起来像这样的 graphql 响应:

{
    ethereum: {
        address: [{
            address: "0x980c55e501d2fad60b5702e2fd88232137e12145",
            balances: [{
                currency: {
                    address: "0x2a9718deff471f3bb91fa0eceab14154f150a385",
                    name: "ElonGate"
                }
            }, {
                currency: {
                    address: "0xad90c05bc51672eedfee36e58b3ff1a78bbc146d",
                    name: "XSPACE"
                }
            }, {
                currency: {
                    address: "0x33a3d962955a3862c8093d1273344719f03ca17c",
                    name: "SporeFinance"
                }
            }, {
                currency: {
                    address: "0x85102c0062aa918cb9e26d94a284aafca602df13",
                    name: "Avalon Protocol"
                }
            }, {
                currency: {
                    address: "0x07af67b392b7a202fad8e0fbc64c34f33102165b",
                    name: "Aquagoat"
                }
            }, {
                currency: {
                    address: "0x31045e7023e6c388f9447e632a3f9eaff90393fa",
                    name: "Safe Token"
                }
            }, {
                currency: {
                    address: "-",
                    name: "Binance Smart Chain Native Token"
                }
            }, {
                currency: {
                    address: "0x2222227e22102fe3322098e4cbfe18cfebd57c95",
                    name: "Alien Worlds Trilium"
                }
            }, {
                currency: {
                    address: "0xdf6d9fc61bd8163b59d6381ec93ae1d3a4d95bb2",
                    name: "BlimpsRock"
                }
            }, {
                currency: {
                    address: "0x2e291e1c9f85a86d0c58ae15621aac005a8b2ead",
                    name: "Zeppelin.dao"
                }
            }, {
                currency: {
                    address: "0x6b51231c43b1604815313801db5e9e614914d6e4",
                    name: "SafeGalaxy"
                }
            }, {
                currency: {
                    address: "0x8076c74c5e3f5852037f31ff0093eeb8c8add8d3",
                    name: "SafeMoon"
                }
            }, {
                currency: {
                    address: "0xdbaaa36b347d56b77ce0e36f050fceebbf9fbc38",
                    name: "SavePlanetEarth"
                }
            }, {
                currency: {
                    address: "0x3ad9594151886ce8538c1ff615efa2385a8c3a88",
                    name: "SafeMars"
                }
            }, {
                currency: {
                    address: "0xf7844cb890f4c339c497aeab599abdc3c874b67a",
                    name: "NFTArt.Finance"
                }
            }, {
                currency: {
                    address: "0xa57ac35ce91ee92caefaa8dc04140c8e232c2e50",
                    name: "Pitbull"
                }
            }]
        }]
    }
}

I need to get access to all of the address fields under currency.我需要访问货币下的所有地址字段。

Using this:使用这个:

var x = body.data.ethereum.address[0].balances

I can get it down to the currency level:我可以把它降低到货币水平:

[{
    currency: {
        address: "0x2a9718deff471f3bb91fa0eceab14154f150a385",
        name: "ElonGate"
    }
}, {
    currency: {
        address: "0xad90c05bc51672eedfee36e58b3ff1a78bbc146d",
        name: "XSPACE"
    }
}, {
    currency: {
        address: "0x33a3d962955a3862c8093d1273344719f03ca17c",
        name: "SporeFinance"
    }
}, {
    currency: {
        address: "0x85102c0062aa918cb9e26d94a284aafca602df13",
        name: "Avalon Protocol"
    }
}, {
    currency: {
        address: "0x07af67b392b7a202fad8e0fbc64c34f33102165b",
        name: "Aquagoat"
    }
}, {
    currency: {
        address: "0x31045e7023e6c388f9447e632a3f9eaff90393fa",
        name: "Safe Token"
    }
}, {
    currency: {
        address: "-",
        name: "Binance Smart Chain Native Token"
    }
}, {
    currency: {
        address: "0x2222227e22102fe3322098e4cbfe18cfebd57c95",
        name: "Alien Worlds Trilium"
    }
}, {
    currency: {
        address: "0xdf6d9fc61bd8163b59d6381ec93ae1d3a4d95bb2",
        name: "BlimpsRock"
    }
}, {
    currency: {
        address: "0x2e291e1c9f85a86d0c58ae15621aac005a8b2ead",
        name: "Zeppelin.dao"
    }
}, {
    currency: {
        address: "0x6b51231c43b1604815313801db5e9e614914d6e4",
        name: "SafeGalaxy"
    }
}, {
    currency: {
        address: "0x8076c74c5e3f5852037f31ff0093eeb8c8add8d3",
        name: "SafeMoon"
    }
}, {
    currency: {
        address: "0xdbaaa36b347d56b77ce0e36f050fceebbf9fbc38",
        name: "SavePlanetEarth"
    }
}, {
    currency: {
        address: "0x3ad9594151886ce8538c1ff615efa2385a8c3a88",
        name: "SafeMars"
    }
}, {
    currency: {
        address: "0xf7844cb890f4c339c497aeab599abdc3c874b67a",
        name: "NFTArt.Finance"
    }
}, {
    currency: {
        address: "0xa57ac35ce91ee92caefaa8dc04140c8e232c2e50",
        name: "Pitbull"
    }
}]

But from here I can't access all of the address fields.但是从这里我无法访问所有的地址字段。 I can access the first address by using:我可以使用以下方法访问第一个地址:

body.data.ethereum.address[0].balances[0].currency.address

But ideally I need to get all address fields.但理想情况下,我需要获取所有地址字段。

Am I missing something here?我在这里错过了什么吗?

Since body.data.ethereum.address[0].balances is an array of objects body.data.ethereum.address[0].balances[0].currency.address is only going to access the very first of those objects.由于body.data.ethereum.address[0].balances是一个对象数组body.data.ethereum.address[0].balances[0].currency.address只会访问这些对象中的第一个。

You could use Array.map to get all addresses:您可以使用Array.map获取所有地址:

 const input = { ethereum: { address: [{ address: "0x980c55e501d2fad60b5702e2fd88232137e12145", balances: [{ currency: { address: "0x2a9718deff471f3bb91fa0eceab14154f150a385", name: "ElonGate" } }, { currency: { address: "0xad90c05bc51672eedfee36e58b3ff1a78bbc146d", name: "XSPACE" } }, { currency: { address: "0x33a3d962955a3862c8093d1273344719f03ca17c", name: "SporeFinance" } }, { currency: { address: "0x85102c0062aa918cb9e26d94a284aafca602df13", name: "Avalon Protocol" } }, { currency: { address: "0x07af67b392b7a202fad8e0fbc64c34f33102165b", name: "Aquagoat" } }, { currency: { address: "0x31045e7023e6c388f9447e632a3f9eaff90393fa", name: "Safe Token" } }, { currency: { address: "-", name: "Binance Smart Chain Native Token" } }, { currency: { address: "0x2222227e22102fe3322098e4cbfe18cfebd57c95", name: "Alien Worlds Trilium" } }, { currency: { address: "0xdf6d9fc61bd8163b59d6381ec93ae1d3a4d95bb2", name: "BlimpsRock" } }, { currency: { address: "0x2e291e1c9f85a86d0c58ae15621aac005a8b2ead", name: "Zeppelin.dao" } }, { currency: { address: "0x6b51231c43b1604815313801db5e9e614914d6e4", name: "SafeGalaxy" } }, { currency: { address: "0x8076c74c5e3f5852037f31ff0093eeb8c8add8d3", name: "SafeMoon" } }, { currency: { address: "0xdbaaa36b347d56b77ce0e36f050fceebbf9fbc38", name: "SavePlanetEarth" } }, { currency: { address: "0x3ad9594151886ce8538c1ff615efa2385a8c3a88", name: "SafeMars" } }, { currency: { address: "0xf7844cb890f4c339c497aeab599abdc3c874b67a", name: "NFTArt.Finance" } }, { currency: { address: "0xa57ac35ce91ee92caefaa8dc04140c8e232c2e50", name: "Pitbull" } }] }] } }; const addresses = input.ethereum.address[0].balances.map(x => x.currency.address); console.log(addresses);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM