wankdanker/node-object-mapper

Including parent items in arrays

RJFoster opened this issue · 6 comments

Is it possible to include parent items when mapping arrays
For example, if I have:

{
    "a": "AA",
    "b": "BB",
    "array": [
        { "x": "one" },
        { "x": "two" }
    ]
}

and I want to convert it to:

{
    "results": [
        {
            "aye": "AA",
            "bee": "BB",
            "ex": "one"
        },
        {
            "aye": "AA",
            "bee": "BB",
            "ex": "two"
        }
    ]
}

I've tried to use a map something like:

map = {
    "a": "results[].aye",
    "b": "results[].bee",
    "array[].x": "results[].ex"
}

But I get:

{
    results: [ 
        { aye: 'AA', bee: 'BB', ex: 'one' },
        { ex: 'two' } 
    ]
}

Obviously the second array element isn't what I want. Presumably I could use a transform, but with more complex examples I would essentially just be mapping using JavaScript

I have same requirement

Have you guys figured out how to work with this?

This is a good point. I have a version working, but there is a trick. The map has to have the instruction to create all array nodes first, then to populate, as follows:

map = {
    "array[].x": "results[].ex",
    "a": "results[].aye",
    "b": "results[].bee"
}

This would be a new feature, and it breaks a couple of existing tests. @wankdanker what are your thoughts about this?

Any updates on this? I am having similar requirement.

I've fixed it on my own. I've also launched the complete test suite successfully

Hi @emmartu , i have same requirement, could you share your knowledge, Thank you.

I've fixed it on my own. I've also launched the complete test suite successfully