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