简体   繁体   English

使用许多嵌套的 forEach 处理对象的更好方法?

[英]A better way to deal with objects using many nested forEach?

I would like to know if there is a better way to deal with nested forEach when it comes to dealing with objects with properties that are nested arrays themselves.我想知道在处理具有嵌套 arrays 属性的对象时是否有更好的方法来处理嵌套的 forEach。

My object (summarized):我的 object(总结):

{
 ...,
 "tx_responses": [
   {
     ...
     "logs" : [
       {
         ...,
         "events": [
             {
                 "type": "coin_received",
                 "attributes": [
                     {
                         "key": "receiver",
                         "value": "somesome"
                     },
                     {
                         "key": "amount",
                         "value": "somesome"
                     }
                 ]
             },
             ...
             {
                 "type": "transfer",
                 "attributes": [
                     {
                         "key": "recipient",
                         "value": "somesome"
                     },
                     {
                         "key": "sender",
                         "value": "somesome"
                     },
                     {
                         "key": "amount",
                         "value": "somesome"
                     }
                 ]
             },
             {
                 "type": "withdraw_rewards",
                 "attributes": [
                     {
                         "key": "amount",
                         "value": "somesomesomehere"
                     },
                     {
                         "key": "validator",
                         "value": "somesome"
                     }
                 ]
             },
             ...
         ]
     }
 ],
...

I am essentially trying to extract all { key: 'amount', value: 'somesomesomehere' } objects in the "attributes" array of the "type": "withdraw_rewards" object in the "events" array.我实际上是在尝试提取"type": "withdraw_rewards""attributes"数组中的所有{ key: 'amount', value: 'somesomesomehere' }对象: "events"数组中的“withdraw_rewards”object。

Currently this is the code I wrote to carry out my task:目前这是我为执行任务而编写的代码:

getWithdrawnAmounts: async(del_addr_) => {
    let withdrawnAmounts = [];
    const res = await axios.get("some_url_that_uses_del_addr_");
    res.data.tx_responses.forEach(txr => {
        txr.logs.forEach(log => {
            log.events.forEach(evnt => {
                if (evnt.type == "withdraw_rewards") {
                    evnt.attributes.forEach(attr => {
                        if (attr.key == "amount") {
                            withdrawnAmounts.push(attr);
                        }
                    })
                }
            })
        })
    });
    return withdrawnAmounts;
}

The code helps me to get what I need, but I was wondering if there is a better way to write my code so that I dont have to use so many nested .forEach methods.该代码帮助我获得我需要的东西,但我想知道是否有更好的方法来编写我的代码,这样我就不必使用这么多嵌套.forEach方法。 I was wondering if I should use .flat() or .flatMap() but I'm curious to know how would people approach this?我想知道是否应该使用.flat().flatMap()但我很想知道人们将如何处理这个问题?

Thank you!谢谢!

You are going to have to call some kind of iteration for each level you're going deeper.您将不得不为要深入的每个级别调用某种迭代。

Now, there is a more functional way to get the desired data, using flatMap and filter :现在,有一种更实用的方法来获取所需的数据,使用flatMapfilter

 const data = { "tx_responses": [{ "logs": [{ "events": [{ "type": "coin_received", "attributes": [{ "key": "receiver", "value": "somesome" }, { "key": "amount", "value": "somesome" }]}, { "type": "transfer", "attributes": [{ "key": "recipient", "value": "somesome" }, { "key": "sender", "value": "somesome" }, { "key": "amount", "value": "somesome" }]}, { "type": "withdraw_rewards", "attributes": [{ "key": "amount", "value": "somesomesomehere" }, { "key": "validator", "value": "somesome" }]}]}]}]}; const result = data.tx_responses.flatMap(r => r.logs.flatMap(l => l.events.filter(e => e.type === 'withdraw_rewards').flatMap(e => e.attributes.filter(a => a.key === 'amount')) ) ); console.log(result);

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

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