0

Suppose to have an array of objects, as for example:

var arr1 = [{t:new Date('2020-09-12'),NO:2},{t:new Date('2020-09-14'),NO2:20}];
var arr2 = [{t:new Date('2020-09-12'),CO:2}];

I want to filter elements of arr1 using elements of arr2 such that to exclude that with the same values of t. I saw a similar answer, but for simple arrays, where someone suggested to use includes. I wonder if it is possible to use a similar approach. I tried:

var arr1 = [{
    t: new Date('2020-09-12'),
    NO: 2
  }, {
    t: new Date('2020-09-14'),
    NO2: 20
  }],
  arr2 = [{
    t: new Date('2020-09-12'),
    CO: 2
  }],
  res = arr1.filter(f => !arr2.includes(f.t));
console.log(res);

But it does not work. Any suggestion? Thanks in advance.

deepak
  • 1,390
  • 1
  • 8
  • 12
EanX
  • 475
  • 4
  • 21

4 Answers4

2

You could map the arr2 into array of time and use that to exclude time from arr1 using includes

const arr1 = [
  { t: new Date("2020-09-12"), NO: 2 },
  { t: new Date("2020-09-14"), NO2: 20 },
]
const arr2 = [{ t: new Date("2020-09-12"), CO: 2 }]

const res = arr1.filter(
  ({ t }) => !arr2.map(({ t }) => t.getTime()).includes(t.getTime())
)

console.log(res)
hgb123
  • 13,869
  • 3
  • 20
  • 38
1

you can use filter simply to get result.

var arr1 = [{
    t: new Date('2020-09-12'),
    NO: 2
  }, {
    t: new Date('2020-09-14'),
    NO2: 20
  }],
  arr2 = [{
    t: new Date('2020-09-12'),
    CO: 2
  }],
  res = arr1.filter(f => arr2.filter(time => time.t.getTime() != f.t.getTime()).length);
console.log(res);
deepak
  • 1,390
  • 1
  • 8
  • 12
1

To build on what you have if you want to use include you need to have a simpler array or use a different function like some to check if that value exists in any of the objects in the array.

var arr1 = [{t:new Date('2020-09-12'),NO:2},{t:new Date('2020-09-14'),NO2:20}],
var filterDates = [{t:new Date('2020-09-12'),CO:2}].map(v=>v.t),
res = arr1.filter(f => !filterDates.includes(f.t));

or

var arr1 = [{t:new Date('2020-09-12'),NO:2},{t:new Date('2020-09-14'),NO2:20}],
res = arr1.filter(f => !arr2.some(v=>v.t===f.t));
  • Thanks, your second solution is very concise, but work correctly only using .getTime for date as: `res = arr1.filter(f => !arr2.some(v=>v.t.getTime()===f.t.getTime()));` Same hold true, actually, for the first one. – EanX Sep 30 '20 at 09:19
0

You can use the array.filter property of arrays to filter out non similar objects using the t key as check Eg here ::

var arr1 = [{t:new Date('2020-09-12'),NO:2},{t:new Date('2020-09-14'),NO2:20}]; 
var arr2 = [{t:new Date('2020-09-12'),CO:2}];

const uniqueElementsArray = arr1.filter(element => {
  // Checking if an element in arr1 is also in arr2
  let isDuplicate = arr2.some(e => e.t.getTime() === element.t.getTime())
  return !isDuplicate
})

console.log(uniqueElementsArray)
 // Would return the array below
 // [ { t: 2020-09-14T00:00:00.000Z, NO2: 20 } ]

The Array.some METHOD checks whether at least one of the elements in arr2 has the same time (t) as the current element of Arr1 that Array.filter is currently on. Read more on Array.some HERE

t.getTime() was used to convert the date (t) of an element to milliseconds to make compare the dates

Rilla
  • 505
  • 4
  • 16