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?

1

4 Answers 4

3

Try this:

const newArr = [];

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

arr.forEach((el) => {
  if (brrDateTimes.indexOf(el.dateTime) == -1) 
    newArr.push(el)
});
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

1
time=brr.map(x=>x.dateTime);
arr.map(x=>!time.includes(x.dateTime))

Comments

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

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.