使用另一个json对象从json对象中删除特定内容

时间:2019-03-22 09:49:19

标签: javascript json

我想做的事情很难解释...

这是一般想法:

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

我只使用过数字属性。其余的不管其他属性如何仍然存在。它是抓取的内容。

我想使用另一个对象作为要删除的对象的引用,从对象中删除项目。请记住,这都是动态的,因此我可能在一个对象中包含3个项目,在另一个对象中包含100个项目。

我需要检查对象1并查看它是否包含对象2中的任何内容,如果要包含,则我想从对象1中删除对象2中的项目,然后从对象1中输出其余项目。令人困惑...

任何帮助将不胜感激。

6 个答案:

答案 0 :(得分:1)

您可以减少object1并使用find来查看数字是否包含在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)

答案 1 :(得分:0)

这是使用lodash的differenceWith函数可以实现的方式:

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

答案 2 :(得分:0)

您需要一个比较两个对象的函数,然后可以过滤找到的对象:

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

如果numer是对象的唯一ID,则可以过滤:

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

答案 3 :(得分:0)

可以结合使用Array.filter和Array.findIndex函数来实现它

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)

答案 4 :(得分: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;
});

答案 5 :(得分:0)

这是另一种选择,用于从object1数组中删除与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))
);