0

I have an array of object, which has be combined based on a unique attribute. For example:

[
  { "macId": "123", "input": 30},
  { "macId": "567", "input": 40},
  { "macId": "123", "power": 100},
  { "macId": "567", "power": 250}
]

I want the resultant array of object as the following:

[
  { "macId": "123", "input": 30, "power": 100},
  { "macId": "567", "input": 40 "power": 250}
]

Is this possible with lodash? If not, how can I write a short code for this to do so?

3
  • I'm almost sure you can do something with Array.reduce and some algorithm Commented Aug 24, 2017 at 9:57
  • 1
    Have look at lodash's merge Commented Aug 24, 2017 at 10:01
  • Just a reminder @chetanraj. There are multiple answers here which answer your question. I suggest you pick one as the accepted answer so we can mark this as solved. (Or if you don't think this question is solved, maybe you could clarify how the answers misunderstand your question, so that others can try to answer your question with this new information in mind). Commented Aug 24, 2017 at 23:20

4 Answers 4

2

Using lodash I would try groupBy using macId, then using merge to "combine" the objects in the resulting array.

const after = 
  _(before)
    .groupBy('macId')
    .map(group => _.merge(...group))
    .value();
Sign up to request clarification or add additional context in comments.

Comments

1

I would suggest using an ES6 Map in combination with Object.assign:

const arr = [
    { "macId": "123", "input": 30},
    { "macId": "567", "input": 40},
    { "macId": "123", "power": 100},
    { "macId": "567", "power": 250}
]

const result = [...arr.reduce((acc, obj) =>
    acc.set(obj.macId, Object.assign(acc.get(obj.macId) || {}, obj)), new Map).values()];

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Comments

0

You can do the following, with Object#assign and Array#find :

var myMergedArray = arr.reduce((acc = [], val) => {
  if (acc.filter(obj => obj.macId === val.macId).length !== 0) {
    Object.assign(acc.find((el) => el.macId === val.macId), val)
  } else {
    acc.push(val);
  }
  return acc;
}, [])

Snippet :

var arr = [
  { "macId": "123", "input": 30},
  { "macId": "567", "input": 40},
  { "macId": "123", "power": 100},
  { "macId": "567", "power": 250}
]

var myMergedArray = arr.reduce((acc = [], val) => {
  if (acc.filter(obj => obj.macId === val.macId).length !== 0) {
    Object.assign(acc.find((el) => el.macId === val.macId), val)
  } else {
    acc.push(val);
  }
  return acc;
}, [])

console.log(myMergedArray);

Comments

0

Since you have tagged lodash you can easily group it and map it back (merge using reduce or _.merge) easily.

var arr = [
  { "macId": "123", "input": 30},
  { "macId": "567", "input": 40},
  { "macId": "123", "power": 100},
  { "macId": "567", "power": 250}
]

var unique = _.chain(arr).groupBy('macId').map(v => _.merge(...v)).value();

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

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.