Delete specific contents from a json object using another json object

问题: 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},{"nu...

问题:

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.


回答1:

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)


回答2:

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)


回答3:

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

回答4:

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

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


回答5:

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


回答6:

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

  • 发表于 2019-03-23 18:11
  • 阅读 ( 173 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除