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

Yet another array merging issue: root node array #32

Closed
noraj opened this issue May 14, 2021 · 2 comments
Closed

Yet another array merging issue: root node array #32

noraj opened this issue May 14, 2021 · 2 comments

Comments

@noraj
Copy link

noraj commented May 14, 2021

I'm seen most of the issues are related to merging arrays but yet I didn't find the solution reading them.

The issue: root node array

root node array is not merging properly like is does when it's a sub-node array

file1.json

[
  {"title": "A"},
  {"title": "B"}
]

file2.json

[
  {"title": "C"},
  {"title": "D"}
]

expected result

[
  {"title": "A"},
  {"title": "B"},
  {"title": "C"},
  {"title": "D"}
]

with the 4 possible combinations of concatArrays, + mergeArrays

concatArrays mergeArrays output
true true
true false
false true
false false
{
  "0": {
    "title": "C"
  },
  "1": {
    "title": "D"
  }
}

Similar but not exactly the same issues

Working example of sub-node array

file1.json

{
  "array": [
    {"title": "A"},
    {"title": "B"}
  ]
}

file2.json

{
  "array": [
    {"title": "C"},
    {"title": "D"}
  ]
}

concatArrays: true + mergeArrays: true

{
  "array": [
    {
      "title": "A"
    },
    {
      "title": "B"
    },
    {
      "title": "C"
    },
    {
      "title": "D"
    }
  ]
}
@noraj noraj changed the title Yet another array merging issue Yet another array merging issue: root node array May 14, 2021
@joshswan
Copy link
Owner

You need to override the startObj for root level arrays or it attempts to merge those arrays into the default starting object: {}. There's a test case that covers this scenario and looks like:

gulp.src(['test/json/array1.json', 'test/json/array2.json']).pipe(merge({
  startObj: [],
}));

Let me know if you have further issues and happy to accept a PR for improving the docs if needed :)

@noraj
Copy link
Author

noraj commented May 15, 2021

@joshswan Thanks.

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

2 participants