0

If x property value matches in both the objects in 2 arrays. merge those objects into single object.

var data3= [{"x":"Mfg1","y":83840.901245},{"x":"Mfg2","y":74804.973325},{"x":"Mfg3","y":81869.98221666666},{"x":"Mfg4","y":36503.152352499994}];
var data4 = [{"x":"Mfg1","y":83840.901245},{"x":"Mfg2","y":74804.973325},{"x":"Mfg8","y":51869.98221666666},{"x":"Mfg4","y":36503.152352499994}];

output array should be:

[{"x":"Mfg1","y":83840.901245},{"x":"Mfg2","y":74804.973325},{"x":"Mfg3","y":81869.98221666666},{"x":"Mfg4","y":36503.152352499994},{"x":"Mfg8","y":51869.98221666666}]
5
  • 3
    Nice! So, what have you already tried? Commented Jun 28, 2018 at 10:42
  • 1
    Plus a proper formatting could help us better understand your code ;) Commented Jun 28, 2018 at 10:43
  • _.unionBy(obj1, obj2, key) ..I think it works Commented Jun 28, 2018 at 10:45
  • Feels like Lodash.. But please read this: stackoverflow.com/help/how-to-ask Commented Jun 28, 2018 at 10:46
  • now check the question dude. I rephrased it ... Commented Jun 28, 2018 at 11:05

1 Answer 1

1

You could use the stringified version of theobject to check for duplicates:

const unique = (getKey, s = new Set) => el => !s.has(getKey(el)) && s.add(getKey(el));

const result = data3.concat(data4).filter( unique(el => JSON.stringify(el)) );
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot..jonas

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.