javascript - Get unique values by comparing old and new array objects - Stack Overflow

Compare two array of objects to find distinct values by key number Suppose the old object consists of o

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" }]

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" }]
Share Improve this question edited Sep 11, 2019 at 13:30 N3R4ZZuRR0 2,4224 gold badges20 silver badges32 bronze badges asked Sep 11, 2019 at 12:31 Roshan SuvarnaRoshan Suvarna 611 silver badge7 bronze badges 3
  • what do you mean with index number? is the new array always greater than the original array? what should happen, if not? – Nina Scholz Commented Sep 11, 2019 at 12:33
  • newChoices will always be greater than the oldChoices. – Roshan Suvarna Commented Sep 11, 2019 at 12:36
  • This might be helpful stackoverflow./questions/51109445/… – Casper Commented Sep 11, 2019 at 12:36
Add a ment  | 

4 Answers 4

Reset to default 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);

Your second attempt is close, just change to:

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

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);

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 parison function:

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

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745079401a4610030.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信