0

there is a way for merge array items by first item

_.merge({a: 1, c: [{y: 8}, {z: 9}]}, {b: 0, c: [{x: 5}]})

Result:

{
   "a": 1,
   "c": [
      {
         "y": 8,
         "x": 5
      },
      {
         "z": 9
      }
   ],
   "b": 0
}

what i want:

{
   "a": 1,
   "c": [
      {
         "y": 8,
         "x": 5
      },
      {
         "z": 9,
         "x": 5 // <-------------------------
      }
   ],
   "b": 0
}

I want merge a source object by another used like a model. In case of array the model define only the first item of the collection and the source object should reflect the first model item into all the collection items.

4
  • 4
    “Expected” based on what? What should be the logic behind this? Your example seems arbitrary to me. Commented Jul 15, 2019 at 13:21
  • Based on the example, it adds the X:5 to the first item in the array. If you want to push it through all array elements you may want to have the c array, be a bunch of : c: [{x:5},{x:5},...]... because as of right now the result makes sense. You would just need to make your array have the same length as the rest of c one would think. Another option would be to merge everything, less that object, and then carry out custom functionality. Commented Jul 15, 2019 at 13:26
  • I don’t think what you want still deserves to be called “merging”, at least not in the way that term is usually understood and used. You might have to write your own little function to achieve this rather special case. Commented Jul 15, 2019 at 13:39
  • @misorude ok, i understood. Thanks. Commented Jul 15, 2019 at 13:45

2 Answers 2

1

I wouldn't actually do it, as it might be very confusing (especially when you've got an array with more than 1 item).

You can use _.mergeWith(), and manually iterate and merge array items.

const mrg = (o1, o2) => _.mergeWith(
  o1, o2,
  (a, b) => _.isArray(a) && _.isArray(b) ?
    a.map((item, i) => mrg(item, b[Math.min(i, b.length - 1)]))
    : 
    undefined
)

const result = mrg({a: 1, c: [{y: 8}, {z: 9}]}, {b: 0, c: [{x: 5}]})

console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.14/lodash.js"></script>

Sign up to request clarification or add additional context in comments.

Comments

0

Use _.mergeWith

function customizer(objValue, srcValue) {
    if (_.isArray(objValue) && _.every(objValue, _.isObject) &&
        _.isArray(srcValue) && _.every(srcValue, _.isObject) )
    {   
        let newObj = Object.assign(...srcValue)
        _.forEach(objValue, function(obj, index) {
            objValue[index] = Object.assign(obj, newObj)
        });
    }
}

let a = {a: 1, c: [{y: 8}, {z: 9}]}
let b = {b: 0, c: [{x: 5}]}
let res = _.mergeWith(a, b, customizer)

Result:

{
    "a": 1,
    "c": [
        {
            "y": 8,
            "x": 5
        },
        {
            "z": 9,
            "x": 5
        }
    ],
    "b": 0
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.