2

Compare two array of objects to find distinct values by key number

Suppose the old object consists of

oldChoices = [{"number": 1, "text": "abc" }, {"number": 2, "text": "pqr" }]

and new object consists of

newChoices = [{"number": 1, "text": "abc" }, {"number": 2, "text": "pqr" }, {"number": 3, "text": "xyz" }]

So need to get:

[{"number": 3, "text": "xyz" }]

Note: 1. Values populate in the newChoices array on the keypress event of the textbox. 2. newChoices can get value at the start as well.

Attempt 1:

var uniqueTemp = [];
$.each(oldChoices, function(x, e1){
   $.each(newChoices, function(y, e2){
      if(e1.number != e2.number){
         uniqueTemp.push(e2);
      }
   });
})

Attempt 2:

var uniqueTemp = [];
oldChoices.filter(function(x){
   if(newChoices.indexOf(x.number) === -1){
    uniqueTemp.push(x);
    return true;
   }else{
    return false;
   }
});

Expected:

[{"number": 3, "text": "xyz" }]
3
  • what do you mean with index number? is the new array always greater than the original array? what should happen, if not? Commented Sep 11, 2019 at 12:33
  • newChoices will always be greater than the oldChoices. Commented Sep 11, 2019 at 12:36
  • This might be helpful stackoverflow.com/questions/51109445/… Commented Sep 11, 2019 at 12:36

4 Answers 4

3

You could take a Set and filter the new array.

var oldChoices = [{ number: 1, text: "abc" }, { number: 2, text: "pqr" }],
    newChoices = [{ number: 1, text: "abc" }, { number: 2, text: "pqr" }, { number: 3, text: "xyz" }],
    old = new Set(oldChoices.map(({ number }) => number)),
    result = newChoices.filter(({ number }) => !old.has(number));

console.log(result);

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

Comments

3

Your second attempt is close, just change to:

newChoices.filter((x) => {
   return (!oldChoices.find((choice) => choice.number === x.number));
});

Comments

1

Here is your solution . Simple use the flag for it .
in arr you will have a unique object as expected .

var oldChoices = [{"number": 1, "text": "abc" }, {"number": 2, "text": "pqr" }]
var newChoices = [{"number": 1, "text": "abc" }, {"number": 2, "text": "pqr" }, {"number": 3, "text": "xyz" }];
var arr = []
var flag = 0;
newChoices.forEach(function(newChoice){
    oldChoices.forEach(function(oldChoice){
        if(oldChoice.number == newChoice.number){
            flag = 1;
        }
    });
    if(flag != 1){
        arr.push(newChoice);
    }
    flag = 0;
});

console.log(arr);

Comments

0

This is a generic function that calculates the difference of two arrays:

let arrayDifference = (v1, v2, cmp = null) => 
  [...v1.filter(o1 => !v2.some(o2 => cmp ? cmp(o1, o2) : o1 === o2)),  
   ...v2.filter(o1 => !v1.some(o2 => cmp ? cmp(o1, o2) : o1 === o2))]

Than you can invoke it with the right comparison function:

arrayDifference(
  oldChoices,
  newChoices,
  (o1, o2) => o1.number === o2.number
)

This function finds the unique objects that occurs both in oldChoices and in newChoices.

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.