Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

122
Visualizações
Lodash pick nested key

I have following objects:

{
    "coins": [],
    "contract": "terra1u0t35drzyy0mujj8rkdyzhe264uls4ug3wdp3x",
    "execute_msg": {
        "send": {
            "amount": "1000000",
            "contract": "terra1ltnkx0mv7lf2rca9f8w740ashu93ujughy4s7p",
            "msg": "eyJkZXBvc2l0X2NvbGxhdGVyYWwiOnt9fQ=="
        }
    },
    "sender": "terra194rswjxvv0a2fm3f8hr4e4f3443dl3s7frsyhp"
}


{
    "amount": [
        {
            "amount": "1000000",
            "denom": "uusd"
        }
    ],
    "from_address": "terra194rswjxvv0a2fm3f8hr4e4f3443dl3s7frsyhp",
    "to_address": "terra1ux73wdfgmu7r5us2sf0u9vdmrfxuhdk8760zzj"
}

{
    "coins": [
        {
            "amount": "2000000",
            "denom": "uusd"
        }
    ],
    "contract": "terra15dwd5mj8v59wpj0wvt233mf5efdff808c5tkal",
    "execute_msg": {
        "deposit_stable": {}
    },
    "sender": "terra194rswjxvv0a2fm3f8hr4e4f3443dl3s7frsyhp"
}

And from each object i want to extract the amount key and pick the number against that key using lodash. I've try it "findKey" and "pick" operator but i think do not use them correctly, and idea how to to get the most nested amount key

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can get the flattened version of all the pairs of the object using the mixin implemented here.

After that, use a Map to store them and get the value of the amount:

_.mixin({
  toPairsDeep: obj => _.flatMap(
    _.toPairs(obj), 
    ([k, v]) => _.isObjectLike(v) ? _.toPairsDeep(v) : [[k, v]]
  )
});

const getAmount = obj => new Map(_.toPairsDeep(obj)).get("amount");

console.log( getAmount({ "coins": [], "contract": "terra1u0t35drzyy0mujj8rkdyzhe264uls4ug3wdp3x", "execute_msg": { "send": { "amount": "1000000", "contract": "terra1ltnkx0mv7lf2rca9f8w740ashu93ujughy4s7p", "msg": "eyJkZXBvc2l0X2NvbGxhdGVyYWwiOnt9fQ==" } }, "sender": "terra194rswjxvv0a2fm3f8hr4e4f3443dl3s7frsyhp" }) );
console.log( getAmount({ "amount": [ { "amount": "1000000", "denom": "uusd" } ], "from_address": "terra194rswjxvv0a2fm3f8hr4e4f3443dl3s7frsyhp", "to_address": "terra1ux73wdfgmu7r5us2sf0u9vdmrfxuhdk8760zzj" }) );
console.log( getAmount({ "coins": [ { "amount": "2000000", "denom": "uusd" } ], "contract": "terra15dwd5mj8v59wpj0wvt233mf5efdff808c5tkal", "execute_msg": { "deposit_stable": {} }, "sender": "terra194rswjxvv0a2fm3f8hr4e4f3443dl3s7frsyhp" }) );
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js" integrity="sha512-WFN04846sdKMIP5LKNphMaWzU7YpMyCU245etK3g/2ARYbPK9Ub18eG+ljU96qKRCWh+quCY7yefSmlkQw1ANQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda