0

for example

let array1 = [
  { 'id': 1010, 'name': 'grapes' }, 
  { 'id': 2020, 'name': 'blueberry' },
  { 'id': 3030, 'name': 'banana' }
]

let array2 = [
  { 'id': 1010, 'name': 'apple' }, 
  { 'id': 2020, 'name': 'blueberry' }, 
  { 'id': 4040, 'name': 'banana' },
  {'id' : 5050, name 'jackfruit'}
]

output should be

let result = [
  { 'id': 1010, 'name': 'grapes' },
  { 'id': 3030, 'name': 'banana' },
  { 'id': 4040, 'name': 'banana' },
  { 'id' : 5050, name 'jackfruit'}
]

here need to get an array which has uncommon object data ENTRY { 'id': 2020, 'name': 'blueberry' } is removed as id and name are commom in both array

2
  • 1
    apple and grapes have the same id. Is this deliberate? Commented Mar 23, 2020 at 14:13
  • 1
    Another way of asking that: why does your expected result not include { 'id': 1010, 'name': 'apple' }? Commented Mar 23, 2020 at 15:35

2 Answers 2

1

These examples use the dataset from the original question but the logic still stands for the updated question.

Depending on the result you want, you can get the difference between the arrays like this:

const result = _.differenceWith(array1, array2, _.isEqual);

That will output

{ id: 1010, name: "grapes" }

If you want the symmetric difference you can concatenate the opposite as well:

const result = _.differenceWith(array1, array2, _.isEqual).concat(_.differenceWith(array2, array1, _.isEqual));

This will give you

{ id: 1010, name: "grapes" }
{ id: 1010, name: "apple" }
{ id: 3030, name: "banana" }

The result you have quoted in your question is slightly different, it is neither difference or symmetric difference, if you only want one result for each ID you would need to remove the second occurence of any object that has an ID key that already exists like so:

result = result.filter((elm, i) => !result.find((elm2, j) => elm.id === elm2.id && i > j) );

that will give you

{ id: 1010, name: "grapes" }
{ id: 3030, name: "banana" }
Sign up to request clarification or add additional context in comments.

2 Comments

Your fiddle is blocked for me by some corporate firewall, but I get a different result with your symmetric difference call: runkit.com/crosseye/so60812738. Result: 1010 grape, 3030 banana, 1010 apple, 4040 banana, 5050 jackfruit. This is what I would expect, and I don't know why the question is asking for something different.
@ScottSauyet That is because the question has been edited with a new dataset since I answered. However the result you are getting is the symmetric difference of the dataset you are inputting :)
0

Incase you want to roll your own.

The following code finds the union, sorts it by the relevant property, and then traverses it. If a key is the same as the previous, then a duplicate has been found and both elements are removed.

function except(a,b) {
    const union = [...a, ...b]
    union.sort(({id: id1}, {id: id2}) => id1 - id2)
    const except = []
    let previous = null
    for(const el of union) {
        if(el.id === previous) {
            except.pop()
            continue;
        }
        except.push(el)
        previous = el.id
    }
    return except
}

let arr1 = [
  { 'id': 1010, 'name': 'grapes' }, 
  { 'id': 2020, 'name': 'blueberry' },
  { 'id': 3030, 'name': 'banana' }
]

let arr2 = [
  { 'id': 0000, 'name': 'apple' }, 
  { 'id': 2020, 'name': 'blueberry' }, 
  { 'id': 4040, 'name': 'banana' },
  {'id' : 5050, name: 'jackfruit'}
]

console.log(except(arr1, arr2))

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.