12

I want to merge 4 array of object into one array

For example: 4 arrays like

var arr1 =[
  { memberID : "81fs", RatingCW:4.5},
  { memberID : "80fs", RatingCW:4},
  { memberID : "82fs", RatingCW:5 },
  { memberID : "83fs", RatingCW:3},
  { memberID : "84fs", RatingCW:4.7}
];
var arr2 =[
  { memberID : "80fs", ratingWW: 4},
  { memberID : "81fs", ratingWW: 4.5},
  { memberID : "83fs", ratingWW: 3},
  { memberID : "82fs", ratingWW: 5},
  { memberID : "84fs", ratingWW: 3.5}
];

var arr3 =  [
  { memberID : "80fs", incoCW:4},
  { memberID : "81fs", incoCW:4.5},
  { memberID : "82fs", incoCW:5},
  { memberID : "83fs", incoCW:3},
  { memberID : "84fs", incoCW:4.5}
  ];
var arr4 =  [
  { memberID : "80fs", incoWW:3},
  { memberID : "81fs", incoWW:2.5 },
  { memberID : "82fs", incoWW:5 },
  { memberID : "83fs", incoWW:3 },
  { memberID : "84fs", incoWW:6.5 }
];

and expected array like:

var finalArr = [
    { memberID : "80fs", RatingCW:4,ratingWW: 4, incoCW:4, incoWW:3},
    { memberID : "81fs", RatingCW:4.5,ratingWW: 4.5, incoCW:4.5, incoWW:2.5 },
    { memberID : "82fs", RatingCW:5,ratingWW: 5, incoCW:5, incoWW:5 },
    { memberID : "83fs", RatingCW:3,ratingWW: 3, incoCW:3, incoWW:3 },
    { memberID : "84fs", RatingCW:4.7,ratingWW: 3.5, incoCW:4.5, incoWW:6.5 }
  ];

What is the best way to merge using lodash or normal javascript?

1
  • order can be different @charlietfl Commented Jul 21, 2016 at 13:28

5 Answers 5

18

With lodash, a lot more readable I think.

var arr1 = [{"memberID":"81fs","RatingCW":4.5},{"memberID":"80fs","RatingCW":4},{"memberID":"82fs","RatingCW":5},{"memberID":"83fs","RatingCW":3},{"memberID":"84fs","RatingCW":4.7}],
    arr2 = [{"memberID":"80fs","ratingWW":4},{"memberID":"81fs","ratingWW":4.5},{"memberID":"83fs","ratingWW":3},{"memberID":"82fs","ratingWW":5},{"memberID":"84fs","ratingWW":3.5}],
    arr3 = [{"memberID":"80fs","incoCW":4},{"memberID":"81fs","incoCW":4.5},{"memberID":"82fs","incoCW":5},{"memberID":"83fs","incoCW":3},{"memberID":"84fs","incoCW":4.5}],
    arr4 = [{"memberID":"80fs","incoWW":3},{"memberID":"81fs","incoWW":2.5},{"memberID":"82fs","incoWW":5},{"memberID":"83fs","incoWW":3},{"memberID":"84fs","incoWW":6.5}];

var merged = _(arr1)
  .concat(arr2, arr3, arr4)
  .groupBy("memberID")
  .map(_.spread(_.merge))
  .value();

console.log(merged);
<script src="https://cdn.jsdelivr.net/lodash/4.13.1/lodash.min.js"></script>

Here is the codepen: http://codepen.io/kuhnroyal/pen/Wxzdmw

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

5 Comments

Just do .concat(arr2, arr3, arr4). Also, .groupBy('memberID') is shorthand for that grouping function.
You can use _.spread(). e.g. map(_.spread(_.merge)).
Had a very similar issue and would have double looped if not for this solution. Beautiful - thank you for sharing.
Does anyone happen to know if this will maintain a specific order?
Could you please provide an equivalent using _.flow?
6

Vanilla JS way,it's a tough one,took hours to get it.

var arr1 = [{"memberID":"81fs","RatingCW":4.5},{"memberID":"80fs","RatingCW":4},{"memberID":"82fs","RatingCW":5},{"memberID":"83fs","RatingCW":3},{"memberID":"84fs","RatingCW":4.7}],
    arr2 = [{"memberID":"80fs","ratingWW":4},{"memberID":"81fs","ratingWW":4.5},{"memberID":"83fs","ratingWW":3},{"memberID":"82fs","ratingWW":5},{"memberID":"84fs","ratingWW":3.5}],
    arr3 = [{"memberID":"80fs","incoCW":4},{"memberID":"81fs","incoCW":4.5},{"memberID":"82fs","incoCW":5},{"memberID":"83fs","incoCW":3},{"memberID":"84fs","incoCW":4.5}],
    arr4 = [{"memberID":"80fs","incoWW":3},{"memberID":"81fs","incoWW":2.5},{"memberID":"82fs","incoWW":5},{"memberID":"83fs","incoWW":3},{"memberID":"84fs","incoWW":6.5}];

const arrs=[].concat(arr1,arr2,arr3,arr4);
const noDuplicate=arr=>[...new Set(arr)]
const allIds=arrs.map(ele=>ele.memberID);
const ids=noDuplicate(allIds);

const result=ids.map(id=>
    arrs.reduce((self,item)=>{
        return item.memberID===id?
        {...self,...item} : self
    },{})
)
console.log(result);

Comments

5

Here's some steps using lodash:

  1. Put all objects into an array (_.flatten then _.groupby)
  2. Flatten 2d array into 1d array of 'user' obj (_.map then _.merge and apply to pass in array as arg)

var arr1 =[
  { memberID : "81fs", RatingCW:4.5},
  { memberID : "80fs", RatingCW:4},
  { memberID : "82fs", RatingCW:5 },
  { memberID : "83fs", RatingCW:3},
  { memberID : "84fs", RatingCW:4.7}
];
var arr2 =[
  { memberID : "80fs", ratingWW: 4},
  { memberID : "81fs", ratingWW: 4.5},
  { memberID : "83fs", ratingWW: 3},
  { memberID : "82fs", ratingWW: 5},
  { memberID : "84fs", ratingWW: 3.5}
];

var arr3 =  [
  { memberID : "80fs", incoCW:4},
  { memberID : "81fs", incoCW:4.5},
  { memberID : "82fs", incoCW:5},
  { memberID : "83fs", incoCW:3},
  { memberID : "84fs", incoCW:4.5}
  ];
var arr4 =  [
  { memberID : "80fs", incoWW:3},
  { memberID : "81fs", incoWW:2.5 },
  { memberID : "82fs", incoWW:5 },
  { memberID : "83fs", incoWW:3 },
  { memberID : "84fs", incoWW:6.5 }
];

var a  = _.groupBy(_.flatten([arr1,arr2,arr3,arr4]), 'memberID');
var b = _.map(a, function(val){ return _.merge.apply(_,val) });    
console.log(b);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/1.2.1/lodash.min.js"></script>

Comments

2
arr1.map(a =>
    Object.assign({},a,
        arr2.find(b => b.memberID === a.memberID),
        arr3.find(c => c.memberID === a.memberID),
        arr4.find(d => d.memberID === a.memberID)
    )
)

Here is the simplest code ... this is basically a oneliner solution. The performance may not be the best but works well for a small data sets.

1 Comment

cool stuff, can you give more info about how this is bad at performance?
1

You could use a hash table for the right reference to the result array.

I works with unsorted arrays as well, because of the reference memberID in the object.

 var arr1 = [{ memberID: "80fs", RatingCW: 4 }, { memberID: "81fs", RatingCW: 4.5 }, { memberID: "82fs", RatingCW: 5 }, { memberID: "83fs", RatingCW: 3 }, { memberID: "84fs", RatingCW: 4.7 }],
    arr2 = [{ memberID: "80fs", ratingWW: 4 }, { memberID: "81fs", ratingWW: 4.5 }, { memberID: "82fs", ratingWW: 5 }, { memberID: "83fs", ratingWW: 3 }, { memberID: "84fs", ratingWW: 3.5 }],
    arr3 = [{ memberID: "80fs", incoCW: 4 }, { memberID: "81fs", incoCW: 4.5 }, { memberID: "82fs", incoCW: 5 }, { memberID: "83fs", incoCW: 3 }, { memberID: "84fs", incoCW: 4.5 }],
    arr4 = [{ memberID: "80fs", incoWW: 3 }, { memberID: "81fs", incoWW: 2.5 }, { memberID: "82fs", incoWW: 5 }, { memberID: "83fs", incoWW: 3 }, { memberID: "84fs", incoWW: 6.5 }],
    merged = [];

[arr1, arr2, arr3, arr4].forEach(function (a) {
    a.forEach(function (b) {
        if (!this[b.memberID]) {
            this[b.memberID] = {};
            merged.push(this[b.memberID]);
        }
        Object.keys(b).forEach(function (k) {
            this[b.memberID][k] = b[k];
        }, this);
    }, this);
}, Object.create(null));

console.log(merged);

ES6 for unsorted data.

 var arr1 = [{ memberID: "80fs", RatingCW: 4 }, { memberID: "81fs", RatingCW: 4.5 }, { memberID: "82fs", RatingCW: 5 }, { memberID: "83fs", RatingCW: 3 }, { memberID: "84fs", RatingCW: 4.7 }],
    arr2 = [{ memberID: "80fs", ratingWW: 4 }, { memberID: "81fs", ratingWW: 4.5 }, { memberID: "82fs", ratingWW: 5 }, { memberID: "83fs", ratingWW: 3 }, { memberID: "84fs", ratingWW: 3.5 }],
    arr3 = [{ memberID: "80fs", incoCW: 4 }, { memberID: "81fs", incoCW: 4.5 }, { memberID: "82fs", incoCW: 5 }, { memberID: "83fs", incoCW: 3 }, { memberID: "84fs", incoCW: 4.5 }],
    arr4 = [{ memberID: "80fs", incoWW: 3 }, { memberID: "81fs", incoWW: 2.5 }, { memberID: "82fs", incoWW: 5 }, { memberID: "83fs", incoWW: 3 }, { memberID: "84fs", incoWW: 6.5 }],
    merged = [];

[arr1, arr2, arr3, arr4].forEach((hash => a => a.forEach(b => {
    if (!hash[b.memberID]) {
        hash[b.memberID] = {};
        merged.push(hash[b.memberID]);
    }
    Object.assign(hash[b.memberID], b);
}))(Object.create(null)));


console.log(merged);

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.