2

I am looking for a algorithm to get all the object in one array that are not in another array. So given these arrays:

let snap = [
             {
                "text": "pack bags",
                "Id": 0,
                "datetime": "16/10/2020 @ 21:26:2"
             }
           ];

  let data = [
                {
                  "text": "pack bags",
                  "Id": 0,
                  "datetime": "16/10/2020 @ 21:26:2"
                },
                {
                   "text": "booking tickets",
                   "Id": 1,
                   "datetime": "1/11/2020 @ 22:18:17"
                }
            ];

I want to end up with this array:

updateData = [
               {
                 "text": "booking tickets",
                 "Id": 1,
                 "datetime": "1/11/2020 @ 22:18:17"
               }
             ]

5 Answers5

2

You can use Map object to have O(1) while accessing to the items while filtering it:

const maps = new Map(snap.map(s => [s.Id, s]));
const result = data.filter(f => !maps.get(f.Id))

An example:

let snap = [
  {
     "text": "pack bags",
     "Id": 0,
     "datetime": "16/10/2020 @ 21:26:2"
  }
];

let data = [
     {
       "text": "pack bags",
       "Id": 0,
       "datetime": "16/10/2020 @ 21:26:2"
     },
     {
        "text": "booking tickets",
        "Id": 1,
        "datetime": "1/11/2020 @ 22:18:17"
     }
 ];


 const maps = new Map(snap.map(s => [s.Id, s]));
 const result = data.filter(f => !maps.get(f.Id))
 console.log(result)
StepUp
  • 36,391
  • 15
  • 88
  • 148
1

You could use a combination of filter and some to do this:

let snap = [
  {
    text: 'pack bags',
    Id: 0,
    datetime: '16/10/2020 @ 21:26:2',
  },
];

let data = [
  {
    text: 'pack bags',
    Id: 0,
    datetime: '16/10/2020 @ 21:26:2',
  },
  {
    text: 'booking tickets',
    Id: 1,
    datetime: '1/11/2020 @ 22:18:17',
  },
];

console.log(data.filter(d => !snap.some(s => s.Id === d.Id)));
thedude
  • 9,388
  • 1
  • 29
  • 30
0

You can use array filter method. Filter method takes a function to run for every element and returns all the array elements that pass the test.

let snap = [
  {
    text: 'pack bags',
    Id: 0,
    datetime: '16/10/2020 @ 21:26:2',
  },
];

let data = [
  {
    text: 'pack bags',
    Id: 0,
    datetime: '16/10/2020 @ 21:26:2',
  },
  {
    text: 'booking tickets',
    Id: 1,
    datetime: '1/11/2020 @ 22:18:17',
  },
];

const ret = data.filter((x) => snap.filter((y) => y.Id === x.Id).length === 0);
console.log(ret);
mr hr
  • 3,162
  • 2
  • 9
  • 19
0

You can check the complete objects with the solution posted in this question Object comparison in JavaScript Using to compare in your filter function, like:

let snap = [
     {
        "text": "pack bags",
        "Id": 0,
        "datetime": "16/10/2020 @ 21:26:2"
     }
   ];

let data = [
        {
          "text": "pack bags",
          "Id": 0,
          "datetime": "16/10/2020 @ 21:26:2"
        },
        {
           "text": "booking tickets",
           "Id": 1,
           "datetime": "1/11/2020 @ 22:18:17"
        }
    ];
var final = data.filter(function(x) {
    return !snap.some(function(y) {
        return JSON.stringify(x) == JSON.stringify(y);
    })
});
console.log(final);
MarioZ
  • 981
  • 7
  • 16
0
    let snap = [
  {
    text: "pack bags",
    Id: 0,
    datetime: "16/10/2020 @ 21:26:2",
  },
];

let data = [
  {
    text: "pack bags",
    Id: 0,
    datetime: "16/10/2020 @ 21:26:2",
  },
  {
    text: "booking tickets",
    Id: 1,
    datetime: "1/11/2020 @ 22:18:17",
  },
];
let exclude = snap.map((_) => _.Id);
let array = data.filter((_) => !exclude.includes(_.Id));
console.log(array);
Parse Shyam
  • 366
  • 2
  • 14