1

I am little confused on how to compare two array objects' particular value get that neglected values as an array.

var Arr1 = [
 {
  jobId:"j1" 
 },
 {
  jobId:"j2" 
 },
 {
  jobId:"j3" 
 },
 {
  jobId:"j4" 
 },

]

var Arr2 = [
 {
  jobId:"j1" 
 },
 {
  jobId:"j2" 
 },

]

I want my result like this...

//neglected values

[
  {
  jobId:"j3" 
 },
 {
  jobId:"j4" 
 },
]

4 Answers 4

3
  • So let's think about how to go about comparing things in two arrays.
  • It probably makes sense to assume in order to fully compare both arrays, we need to iterate them, making sure we compare every item in each array so that we can be sure
  • So if I was begining i would think of nesting for - loops. Why? Because it will iterate over the first array, on each index it hits the first array, it will then iterate over every element in the second array.
  • then you can create some basic if conditional logic like if Arr1[i] === Arr2[j], push one of the objects into an array.
Sign up to request clarification or add additional context in comments.

2 Comments

@torazaburo Has a better answer for advanced users, but this answer is better considering that the OP is just learning.
@RayfenWindspear I agree, his answer is better, I purposely made it this way because he said he was beginning. I was just trying to outline a solution, because I remember when I started visualizing double for loops helped me understand things better. filtering it out, is a smarter approach for sure.
2

Filter (Array#filter) down the elements of the first array to those for which there are not (!) some (Array#some) matching job IDs in the second array.

Arr1.filter(arr1Elt => !Arr2.some(arr2Elt => arr2Elt.jobId === arr1Elt.jobId))

Using line-by-line comment style:

Arr1                             // From Arr1,
  .filter(                       // keep
    arr1Elt =>                   // elements for which 
      !                          // it is not the case that
      Arr2                       // Arr2 has
        .some(                   // some
          arr2Elt =>             // elements for which 
            arr2Elt.jobId        // the job ID
            ===                  // is equal to
            arr1Elt.jobId        // the job ID from the first array.
        )
  )

Comments

2

You can use lodash's _.differenceBy() to find the items from Arr1 missing from Arr2.

var Arr1 = [{"jobId":"j1"},{"jobId":"j2"},{"jobId":"j3"},{"jobId":"j4"}];
var Arr2 = [{"jobId":"j1"},{"jobId":"j2"}];

// find items that are in Arr1, but not in Arr2, compare items by their jobId
var result = _.differenceBy(Arr1, Arr2 , 'jobId');

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.3/lodash.min.js"></script>

Comments

1

You can use Array.prototype.filter on Arr1 and check if any of its entries are in Arr2 using Array.prototype.some - see demo below:

var Arr1=[{jobId:"j1"},{jobId:"j2"},{jobId:"j3"},{jobId:"j4"}];
var Arr2=[{jobId:"j1"},{jobId:"j2"}];

var result = Arr1.filter(function(e){
  return !Arr2.some(function(k){
    return k.jobId === e.jobId;
  });
});

console.log(result);

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.