3

Is it possible to re-structure the below array

[
    {"period":"2016-09-18","mp-demo-1":30},
    {"period":"2016-09-18","MP7":20},
    {"period":"2016-09-19","mp-demo-1":13},
    {"period":"2016-09-19","MP7":33},
    {"period":"2016-09-20","mp-demo-1":39},
    {"period":"2016-09-20","MP7":29}
]

into

[
    {"period":"2016-09-18","mp-demo-1":30,"MP7":20},
    {"period":"2016-09-19","mp-demo-1":13,"MP7":33},
    {"period":"2016-09-20","mp-demo-1":39,"MP7":29},
]

What I basically need is to remove duplicate entries by the key (period) and add the values containing in those objects in to a single object.

Will this be possible using underscore.js? Or is there another way of doing this using jQuery or any other library.

2 Answers 2

5

You can do this with pure javascript with reduce() and Object.assign()

var data = [
  {"period":"2016-09-18","mp-demo-1":30},
  {"period":"2016-09-18","MP7":20},
  {"period":"2016-09-19","mp-demo-1":13},
  {"period":"2016-09-19","MP7":33},
  {"period":"2016-09-20","mp-demo-1":39},
  {"period":"2016-09-20","MP7":29}
];

var obj = {}
var result = data.reduce(function(r, o) {
  if (!obj[o.period]) {
    obj[o.period] = o;
    r.push(obj[o.period]);
  } else {
    Object.assign(obj[o.period], o);
  }
  return r;
}, []);

console.log(result)

To keep original data you can create clone of object with Object.assign()

var data = [
  {"period":"2016-09-18","mp-demo-1":30},
  {"period":"2016-09-18","MP7":20},
  {"period":"2016-09-19","mp-demo-1":13},
  {"period":"2016-09-19","MP7":33},
  {"period":"2016-09-20","mp-demo-1":39},
  {"period":"2016-09-20","MP7":29}
];

var obj = {}
var result = data.reduce(function(r, o) {
  if (!obj[o.period]) {
    obj[o.period] = Object.assign({}, o);
    r.push(obj[o.period]);
  } else {
    Object.assign(obj[o.period], o);
  }
  return r;
}, []);

console.log(result);
console.log(data);

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

3 Comments

Note that this also modifies the original array.
@georg Updated my answer with solution that keeps original data.
This might work for the OP's data, but in general cloning via serialization is a terrible idea. See my answer for how to do that correctly - basically, you should use Object.assign({}, ...) when you encounter an object for the first time.
2

with lodash:

data = [
    {"period":"2016-09-18","mp-demo-1":30},
    {"period":"2016-09-18","MP7":20},
    {"period":"2016-09-19","mp-demo-1":13},
    {"period":"2016-09-19","MP7":33},
    {"period":"2016-09-20","mp-demo-1":39},
    {"period":"2016-09-20","MP7":29}
];


res = _(data)
    .groupBy('period')
    .map(_.spread(_.merge))
    .value();

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

Vanilla ES6:

data = [
    {"period":"2016-09-18","mp-demo-1":30},
    {"period":"2016-09-18","MP7":20},
    {"period":"2016-09-19","mp-demo-1":13},
    {"period":"2016-09-19","MP7":33},
    {"period":"2016-09-20","mp-demo-1":39},
    {"period":"2016-09-20","MP7":29}
];

let map = new Map();

data.forEach(x => map.set(x.period,
    Object.assign(
        map.get(x.period) || {},
        x
)));

console.log([...map.values()])

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.