Value Setter — access sibling object by key reference

{
    "translations":{
        "attr1":{
            "en":"attr1_en"
        },
        "attr2":{
            "en":"attr2_en"
        }
    },
    "attrs":{
        "attr1":{
            "some_data":"hi"
        },
        "attr2":{
            "some_data":"hi"
        }
    }
}

Are we able to work with the key value?
I meen take value “en” from “translations” based on key name of “attrs” and put value to based obj regards on key
result like:

{
    "translations":{
        "attr1":{
            "en":"attr1_en"
        },
        "attr2":{
            "en":"attr2_en"
        }
    },
    "attrs":{
        "attr1":{
            "some_data":"hi",
            "en":"attr1_en"
        },
        "attr2":{
            "some_data":"hi",
            "en":"attr2_en"
        }
    }
}

Hi @vosy ,

Perhaps you can try this copy pattern ?

Hiiii, damn how simply,
“Copy using a pattern” is a cure for everything :smiley:
How come I didn’t see it?

And now something more complicated,
how to translate keys by “en” like and ut it to new dict attrs_en

"attrs_en":{
    "attr1_en":{
        "some_data":"hi",
        "en":"attr1_en"
    },
    "attr2_en":{
        "some_data":"hi",
        "en":"attr2_en"
    }
}

Need red phone line directly to u :smiley:

Hi @vosy,

Could you please provide the expected result and what it should look like?

Sometimes it’s simple :sweat_smile:

from

"attrs":{
        "attr1":{
            "some_data":"hi",
            "en":"attr1_en"
        },
        "attr2":{
            "some_data":"hi",
            "en":"attr2_en"
        }
    }

to

"attrs_en":{
    "attr1_en":{
        "some_data":"hi",
        "en":"attr1_en"
    },
    "attr2_en":{
        "some_data":"hi",
        "en":"attr2_en"
    }
}

Hi @vosy,

Yes, this one is very interesting case. You can try using “Code transformer” to get the expected result.

Below is the code inside “Code transformer”

function convertAttrsWithDynamicLangKey(input) {
    const result = {};
    const attrs = input.attrs;

    if (!attrs || typeof attrs !== 'object') {
        return result;
    }

    // Try to detect the language key dynamically from the first item
    const firstAttrKey = Object.keys(attrs)[0];
    const firstAttr = attrs[firstAttrKey];

    // Find the language key by excluding known fields (like "some_data")
    const langKey = Object.keys(firstAttr).find(key => key !== "some_data");

    if (!langKey) {
        throw new Error("Language key could not be detected.");
    }

    const resultKey = `attrs_${langKey}`;
    result[resultKey] = {};

    for (const key in attrs) {
        const attr = attrs[key];
        const dynamicKey = attr[langKey];

        if (dynamicKey) {
            result[resultKey][dynamicKey] = attr;
        }
    }

    return result;
}

return convertAttrsWithDynamicLangKey(data);

and is it not able to achieve that over classical data transformer?

create new dict with key by attrs.*.en,

or save attrs to storage with identifier attr.en
it is not bad idea, or is it?

Hi @vosy,

Thank you for your patience. Could you please let us know whether the “en” key is persists and always the case? or is there’re any possibilities that it has other keys than “en”, for example “us”, “uk” etc. ?

if it static (the case is only “en”), then you can try using below transformer :