1

What I wanna do is tricky to explain...

This is the general idea:

var object1 = [{"number":1111,"test":0,"low":2131},{"number":1234,"test":0,"low":2684},{"number":4214,"test":0,"low":6345}];

var object2 = [{"number":1234}];

Some code here

output: var object1 = [{"number":1111,"test":0,"low":2131},{"number":4214,"test":0,"low":6345}];

I am only ever using the number attribute. The rest do not matter how ever the other attributes are still in there regardless. It is scraped content.

I want to remove a item from object using another object as the reference for what i want removed. Keeping in mind, this is all dynamic so i may have 3 items in one object and 100 in the other object.

I need to check object 1 and see if it contains anything from object 2 and If it does then i wanna delete the object 2 items from object 1 then output the remaining items from object 1. Confusing...

Any help would be appreciated.

  • https://stackoverflow.com/questions/10024866/remove-object-from-array-using-javascript – Harshana Mar 22 '19 at 09:53
  • https://stackoverflow.com/questions/1187518/how-to-get-the-difference-between-two-arrays-in-javascript this help? – paqash Mar 22 '19 at 09:55
  • What would happen if the match is partial? Let's say `[{ "number": 1234, "test": 1, "low": 4862 }]`, where *number* matches, but other attributes don't. – 3limin4t0r Mar 22 '19 at 10:02
  • I edited my question. The other attributes are not in the second object. Only the number attribute is in the second object. –  Mar 22 '19 at 10:07

6 Answers6

0

This is how this can be achieved using differenceWith function of lodash:

let difference = _.differenceWith(object1, object2, (obj1, obj2) => {
   return obj1.number === obj2.number
})

Raeesaa
  • 3,267
  • 2
  • 22
  • 44
0

You need a function to compare two object, then you can filter found objects out:

var object1 = [{"number":1111,"test":0,"low":2131},{"number":1234,"test":0,"low":2684},{"number":4214,"test":0,"low":6345}];
var object2 = [{"number":1234,"test":0,"low":2684}];

const objectsEqual = (o1, o2) =>
    Object.keys(o1).length === Object.keys(o2).length 
        && Object.keys(o1).every(p => o1[p] === o2[p]);

var res = object1.filter(o1 => !object2.some(o2 => objectsEqual(o1, o2)));

console.log(res);

If the numer is the unique ID of the object, you can just filter:

var object1 = [{"number":1111,"test":0,"low":2131},{"number":1234,"test":0,"low":2684},{"number":4214,"test":0,"low":6345}];
var object2 = [{"number":1234,"test":0,"low":2684}];

var res = object1.filter(o1 => !object2.some(o2 => o1.number === o2.number));

console.log(res);
ttulka
  • 10,309
  • 7
  • 41
  • 52
0

You can achieve it using combination of Array.filter and Array.findIndex function

var object1 = [{"number":1111,"test":0,"low":2131},{"number":1234,"test":0,"low":2684},{"number":4214,"test":0,"low":6345}];

var object2 = [{"number":1234,"test":0,"low":2684}];


var result = object1.filter(function(obj1){

    return object2.findIndex(function(obj2){
        return obj2.number == obj1.number ;
    }) ==-1;

});

console.log(result)
  • This doesn't work when `var object2 = [{"number":1234,"test":123,"low":1234}];` – ttulka Mar 22 '19 at 10:05
  • This was not clear from the original question. As the question changed the requirement - your solution is okay as well. – ttulka Mar 22 '19 at 10:13
0

You can reduce your object1 and use find too see if the numbers are into object1

var object1 = [{"appid":1111,"test":0,"low":2131},{"appid":1234,"test":0,"low":2684},{"appid":4214,"test":0,"low":6345}];

var object2 = [{"appid":1234}];

var object3 = object1.reduce((ac, v, i) => {
  return object2.find(function(obj) {
    return obj.appid == v.appid;
  }) ? ac :  [...ac, v];
}, [])

console.log(object3)
R3tep
  • 12,512
  • 10
  • 48
  • 75
  • `JSON.stringify` doesn't work when object properties are in different order: `JSON.stringify({"number":1234,"test":0,"low":2684}) !== JSON.stringify({"number":1234,"low":2684,"test":0})` – ttulka Mar 22 '19 at 10:03
  • @3limin4t0r SO demands a minimal example, so we don't know where the objects come from. Maybe from an external resource, serialized as JSON - then it is likely that the order was changed and this solution won't therefore work. It could be fine, I want just to point this fact out. – ttulka Mar 22 '19 at 10:09
  • For some reason when i implement this code, the output is a empty object... though if i test it in jsfiddle it works using the supplied objects but the ones i get when i am scraping should be the same... im not great are coding so sorry if some of this seems a bit confusing. –  Mar 22 '19 at 10:14
  • 1
    No errors. Here is a image of the array outputs in chromes console: https://i.imgur.com/NMOMQlR.jpg That is for the original objects not the code from here. So... i assume they are just objects? I could be wrong. This is the code im using to make them: Im parsing the first "object" which is just a string into json turning it into an object. The second one im pushing the contents into a object. –  Mar 22 '19 at 10:18
  • 1
    @Sean I see the error I updated my post. You need to replace the key number by appid into the find function. – R3tep Mar 22 '19 at 10:18
  • Thank you very much. This is the new response using your code: https://i.imgur.com/ZGhbXsH.jpg I will post a comment if i have further issues with this specific thing. Iv got a few more tests to run while implementing this into a bigger script. so see how it goes. Cheers –  Mar 22 '19 at 10:22
0
let object1 = [{ "number": 1111, "test": 0, "low": 2131 }, { "number": 1234, "test": 0, "low": 2684 }, { "number": 4214, "test": 0, "low": 6345 }];
let object2 = [{ "number": 1234, "test": 0, "low": 2684 }];

// use unique key to findIndex, am using number key here replace this in your case.
object1 = object1.filter(obj => {
    return object2.findIndex(obj1 => {
        return obj.number === obj1.number;
    }) === -1;
});
Boobalan
  • 795
  • 1
  • 5
  • 14
0

Here is another option removing all objects from the object1 array, that match with one or more attributes from the objects in object2.

var object1 = [{"number":1111,"test":0,"low":2131},{"number":1234,"test":0,"low":2684},{"number":4214,"test":0,"low":6345}],
    object2 = [{"number":1234}];

console.log(
  object1.filter(obj1 => {
    return !object2.some(obj2 => {
      for (let attrName in obj1) {
        if (obj1[attrName] === obj2[attrName]) return true;
      }
      return false;
    });
  })
);

// this can be reduced to the following if you don't want to look through
// all attributes, but only a predefined attribute

console.log(
  object1.filter(obj1 => !object2.some(obj2 => obj1.number === obj2.number))
);
3limin4t0r
  • 19,353
  • 2
  • 31
  • 52