EanX
EanX

Reputation: 505

How to filter an arrayof objects from all elements of another array of object on values of a common property

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.

Upvotes: 0

Views: 48

Answers (4)

Rilla
Rilla

Reputation: 534

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

Upvotes: 0

Eli Spiegel
Eli Spiegel

Reputation: 39

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

Upvotes: 1

deepak
deepak

Reputation: 1375

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

Upvotes: 1

hgb123
hgb123

Reputation: 14881

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)

Upvotes: 2

Related Questions