如何从JSON数组中删除JSON对象?

时间:2018-06-19 10:57:44

标签: jquery

var alldesignationsarr = ['Employee','Manager'];

1 个答案:

答案 0 :(得分:0)

您可以使用desgdescarr.filter()



var alldesignationsarr = ['Employee','Manager'];

var desgdescarr = [{
    "designation": {
        "name": "Employee",
        "ptest": {}
    },
    
    "designation": {
        "name": "Manager",
        "ptest": {}
    },
    "designation": {
        "name": "Managers",
        "ptest": {}
    }
}];
    
var res = desgdescarr.filter((obj) => alldesignationsarr.indexOf(obj.designation.name) === -1);
console.log(res);




如果您想对原始数组进行更改,那么您也可以使用简单的forEach逻辑:



var alldesignationsarr = ['Employee','Manager'];

var desgdescarr = [{
    "designation": {
        "name": "Employee",
        "ptest": {}
    },
    
    "designation": {
        "name": "Manager",
        "ptest": {}
    },
    "designation": {
        "name": "Managers",
        "ptest": {}
    }
}];
    
desgdescarr.forEach((obj, index) => {
  if(alldesignationsarr.indexOf(obj.designation.name) !== -1){
    desgdescarr.splice(index,1);
  }
});

console.log(desgdescarr);