Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Including parent items in arrays #50

Open
RJFoster opened this issue May 13, 2018 · 6 comments
Open

Including parent items in arrays #50

RJFoster opened this issue May 13, 2018 · 6 comments

Comments

@RJFoster
Copy link

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

@liangde-chen
Copy link

I have same requirement

@iandecroix
Copy link

Have you guys figured out how to work with this?

switzer pushed a commit to switzer/node-object-mapper that referenced this issue Oct 11, 2019
@switzer
Copy link

switzer commented Oct 11, 2019

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?

@HemalathaBR
Copy link

Any updates on this? I am having similar requirement.

@emmartu
Copy link

emmartu commented Apr 9, 2021

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

@kiennt2
Copy link

kiennt2 commented Mar 29, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants