0

So I've come across this problem when trying to eliminate the need for duplicated data.

Say I've got a list of objects. I'm separating them based on a specific category. And 2 objects return the same data within the Open 'category'. I don't necessarily want to remove the data, so I think merging/combining is the best way?

If Open category, merge all objects. How can I merge objects? I think .concat() is just for arrays. So is this even possible?

...

Open
------
const dupObjects = this.props.response.flight['open'];
// returns objects that look like this...

Object {delta: Array[1], lambda: Array[0], amount: }
Object {delta: Array[1], lambda: Array[0], amount: 200,000} // duplicate lets combine
Object {delta: Array[1], lambda: Array[0], amount: 200,000} // duplicate lets combine



if (category == 'Open'){
    // merge objects
    const dupObjects = this.props.response.flight['open'];
}

Expected output:

Open:
Delta : Information...    Lambda: Information...   Amount: $0
Delta : Information...    Lambda: Information...   Amount: $200,000
7
  • 1
    Please define merge... do you just want to dedupe, or extend one with another? Commented May 11, 2016 at 16:09
  • @DMoses not completely sure tbh. but after a quick google search on those terms, I suppose dedupe? Commented May 11, 2016 at 16:14
  • you might find this of interest: stackoverflow.com/questions/171251/… Commented May 11, 2016 at 16:19
  • @Modelesq Give an example with 3 objects with 2 being the same and the output you would want, otherwise it's hard to tell. Also you may want to look at underscorejs.org for some idea of things that are commonly done. Commented May 11, 2016 at 16:39
  • 1
    Javascript objects do not have "attributes". Do you mean "properties"? Commented May 11, 2016 at 16:47

1 Answer 1

1

What you are asking is to remove duplicate objects where you define a duplicate as having the same values using a deep equal. See http://underscorejs.org/#isEqual Below is a sample script that does dedupe of the list. It's not very elegant, but it works:

var objects = [
  {"delta":["St.Louis"],"lambda":[],"amount":200000},
  {"delta":["St.Louis"],"lambda":[],"amount":200000},
  {"delta":["Different"],"lambda":[],"amount":200000}
];

var dedupedList = _.reduce(objects, 
 function(result, item) { 
  var isDupe = _.any(result, function(anyItem) { return _.isEqual(item, anyItem); });
  if (!isDupe) result.push(item);
  return result; 
 }, 
 []);

console.log(dedupedList);
<script src="//jashkenas.github.io/underscore/underscore-min.js"></script>

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

3 Comments

I really appreciate the answer. Unfortunately I can't use underscore. Since dupObjects's returns each javascript object, I'm just looking to literally combine the 2 or at this point, ignore / remove the duplicate. I'm not sure if it will help me if I post within reactjs channel.
I'm not familiar with reactjs. But, conceptually what I have provided should work. You can write your own dedupeCollection function that does a deep equal and removes any that are already there.
Isn't .push for arrays?

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.