wankdanker / node-object-mapper

Copy properties from one object to another.
MIT License
277 stars 73 forks source link

Problem with array of array #29

Open ghost opened 8 years ago

ghost commented 8 years ago

Hey :) Thanks for such a good lib!

I have a problem when i do this:

{
        "id" : "id",
        "orders[].id": "orders[].id",
        "orders[].productOrders[].id": "orders[].product_orders[].id",
        "orders[].productOrders[].discounts[].id": "orders[].product_orders[].discounts[].id",
}

it outputs this:

[
  {
    "id": "89D743BD-8D02-4893-BBBE-D843C68A4209",
    "orders": [
      {
        "id": "7212C4E0-23B3-4243-BF84-52453392057B",
        "product_orders": [
          {
            "id": "088C32CD-F3B9-4D79-961C-E17F5B80823E",
            "discounts": [
              {
                "id": [
                  "12212C32CD-F3B9-4D79-961C-E17F5"
                ]
              },
              {
                "id": [
                  "89D743BD-8D02-4893-BBBE-D843C68A4209"
                ]
              }
            ]
          },
          {
            "id": "60095821-C470-4847-B66A-84AE5EC98A54"
          }
        ]
      }
    ]
  }
]

The problem is with discounts:

I may have misunderstood the syntax, I tried different ways and it doesn't work :s Have you ever seen this ? Thanks! ;)

briandelancey commented 7 years ago

I'm having essentially the same issue. Here's some sample code:

var objectMapper = require("object-mapper")

var orig = {
    foo: [
        {"name": "a", "things": ["a1", "a2"]},
        {"name": "b", "things": ["b1", "b2"]}
    ]
}

var map = {
    "foo[].name": "bar[].label",
    "foo[].things[]": "bar[].values[]"
};

var newOb = objectMapper(orig, map);

...which gives us this:

bar: [{
        label: "a",
        values: [
            ["a1", "a2", "b1", "b2"]
        ]
    },
    {
        label: "b"
    }
]

...but what's required is this:

bar: [{
        label: "a",
        values: ["a1", "a2"]
    },
    {
        label: "b",
        values: ["b1", "b2"]
    }
]
matmar10 commented 7 years ago

+1 I am facing the same issue. Any idea what the right usage is or possible fix?

matmar10 commented 7 years ago

So this seems to work OK in master, but the 3.0.1 that's tagged in npm does not work (see the test case in my fork). Can you tag a new version so this fix is included?

srogers202 commented 7 years ago

+1. I'm seeing the same issue. Any way we can get a new version tagged?

mnguyen972 commented 7 years ago

Was this array of array issue ever resolved?

jalleyne commented 7 years ago

@briandelancey were you able to get this to work? i have a very similar structure thats still seems to produce similar output as your example.

briandelancey commented 7 years ago

@jalleyne no. My mapping requirements quickly became more complex and I ended up rolling my own solution entirely.

mnguyen972 commented 7 years ago

This issue is resolved for me by taking the code from GitHub rather than getting it from NPM.

wankdanker commented 7 years ago

Hey everyone. So sorry for taking so long for this. If anyone wants to help maintain, lmk.

object-mapper@3.1.0 is on npm

If this resolves this issue, please close it if you can or let me know.

midknight41 commented 7 years ago

@wankdanker I've tried out both 3.0.1 and 3.1.0 with the example provided above and the result is still incorrect as far as I can see.

var objectMapper = require("object-mapper")

var orig = {
    foo: [
        {"name": "a", "things": ["a1", "a2"]},
        {"name": "b", "things": ["b1", "b2"]}
    ]
}

var map = {
    "foo[].name": "bar[].label",
    "foo[].things[]": "bar[].values[]"
};

var newOb = objectMapper(orig, map);

result with 3.0.1

{
    "bar": [{
        "label": "a",
        "values": [
            ["a1", "a2", "b1", "b2"]
        ]
    }, {
        "label": "b"
    }]
}

result with 3.1.0

{
    "bar": [{
        "label": "a",
        "values": [
            [
                ["a1", "a2"],
                ["b1", "b2"]
            ]
        ]
    }, {
        "label": "b"
    }]
}

A corresponding code change would be needed in SetKeyValue I believe.

NoCopy commented 7 years ago

@midknight41 2 notes:

  1. The mapping of the sub array should not have [] at the end: BAD: "foo[].things[]": "bar[].values[]" GOOD "foo[].things[]": "bar[].values" https://runkit.com/nocopy/59fcc300c151150011c902c4

  2. No matter which way you try and structure it, it doesn't seem to work if the initial object is array of objects itself:

    var orig = { // could never get this structure to map / work.
        {"name": "a", "things": ["a1", "a2"]},
        {"name": "b", "things": ["b1", "b2"]}
    }
shwetajoshi601 commented 6 years ago

I am using version 5.0.0. I am still facing the same issue.

switzer commented 5 years ago

Added a test case. Works in my branch.

umangkathuria commented 4 years ago

I am still facing issue with 5.0.0 downloaded through npm. Is this fixed?