0

I have two array of objects containing key-value pairs of dateTime string and count. In arr, I have two objects which have same dateTime values with two of brr's object's dateTime values. I just want to filter out arr's non-equal objects in an array.

What I mean to say is, if my arr is this:

const arr = [
    { dateTime: '2021-08-14 02:00:00', count: 1 },
    { dateTime: '2021-08-15 04:00:00', count: 1 },
    { dateTime: '2021-08-16 10:00:00', count: 1 },
    { dateTime: '2021-08-16 19:00:00', count: 1 },
    { dateTime: '2021-08-17 05:00:00', count: 1 }
];

and brr is this:

const brr = [
    { dateTime: '2021-08-16 19:00:00', count: 1 },
    { dateTime: '2021-08-17 05:00:00', count: 1 }
];

I am trying to get an array like this:

[
    { dateTime: '2021-08-14 02:00:00', count: 1 },
    { dateTime: '2021-08-15 04:00:00', count: 1 },
    { dateTime: '2021-08-16 10:00:00', count: 1 }
];

Failing to do so with this:

const arr = [
    { dateTime: '2021-08-14 02:00:00', count: 1 },
    { dateTime: '2021-08-15 04:00:00', count: 1 },
    { dateTime: '2021-08-16 10:00:00', count: 1 },
    { dateTime: '2021-08-16 19:00:00', count: 1 },
    { dateTime: '2021-08-17 05:00:00', count: 1 }
];

const brr = [
    { dateTime: '2021-08-16 19:00:00', count: 1 },
    { dateTime: '2021-08-17 05:00:00', count: 1 }
];

const newArr = [];

for( let item of brr ) { 

   const filtered = arr.filter( el => {
        return el.dateTime !== item.dateTime;  
   });
   
   newArr.push( filtered );
  
};

console.log( newArr );

I am getting some gibberish in the console. What am I doing wrong here?

s.khan
  • 297
  • 1
  • 7
  • 24
  • 1
    see if this helps you out https://stackoverflow.com/questions/54142112/compare-2-arrays-of-objects-and-remove-duplicates – Andrew Lohr Aug 23 '21 at 17:23

4 Answers4

3

Try this:

const newArr = [];

const brrDateTimes = brr.map((el) => el.dateTime);

arr.forEach((el) => {
  if (brrDateTimes.indexOf(el.dateTime) == -1) 
    newArr.push(el)
});
evanbikes
  • 4,131
  • 1
  • 22
  • 17
2

You could take a set of unwanted dateTime values and filter the array.

const
    array = [{ dateTime: '2021-08-14 02:00:00', count: 1 }, { dateTime: '2021-08-15 04:00:00', count: 1 }, { dateTime: '2021-08-16 10:00:00', count: 1 }, { dateTime: '2021-08-16 19:00:00', count: 1 }, { dateTime: '2021-08-17 05:00:00', count: 1 }],
    filter = [{ dateTime: '2021-08-16 19:00:00', count: 1 }, { dateTime: '2021-08-17 05:00:00', count: 1 }],
    unwanted = new Set(filter.map(({ dateTime }) => dateTime)),
    result = array.filter(({ dateTime }) => !unwanted.has(dateTime));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1
time=brr.map(x=>x.dateTime);
arr.map(x=>!time.includes(x.dateTime))
1

Array.filter operates on an entire array. You don't need to call it inside a loop. Furthermore, when comparing objects for sameness, you need to compare their scalar values. Objects with identical values are still not identical, in Javascript.

const arr = [
    { dateTime: '2021-08-14 02:00:00', count: 1 },
    { dateTime: '2021-08-15 04:00:00', count: 1 },
    { dateTime: '2021-08-16 10:00:00', count: 1 },
    { dateTime: '2021-08-16 19:00:00', count: 1 },
    { dateTime: '2021-08-17 05:00:00', count: 1 }
];

const brr = [
    { dateTime: '2021-08-16 19:00:00', count: 1 },
    { dateTime: '2021-08-17 05:00:00', count: 1 }
];

const diff = arr.filter(a => {
    return !brr.some(b => {
      return a.dateTime === b.dateTime && a.count === b.count;
    });
});

console.log(diff);
code_monk
  • 9,451
  • 2
  • 42
  • 41