javascript 如何从对象数组中删除元素

q7solyqu  于 2023-02-02  发布在  Java
关注(0)|答案(1)|浏览(133)

这里我有对象数据数组,从这个数据中我需要删除基于值的元素,如果找到值,则删除整个元素

let value = "/unifiedconfig/config/agent/5197";

//if this found in json data i have delete complete element{"refURL":"/unifiedconfig/config/agent/5197","agentId":"1085","firstName":"Owen","lastName":"Harvey","userName":"oharvey"}

var myjsonobj = {
  "refURL": "/unifiedconfig/config/agentteam/5022",
  "changeStamp": 12,
  "agentCount": 7,
  "description": "Cumulus Outbound Team",
  "name": "CumulusOutbound",
  "peripheral": {
    "id": 5000,
    "name": "CUCM_PG_1"
  },
  "peripheralId": 5000,
  "supervisorCount": 1,
  "agents": [{
    "agent": [{
      "refURL": "/unifiedconfig/config/agent/5197",
      "agentId": "1085",
      "firstName": "Owen",
      "lastName": "Harvey",
      "userName": "oharvey"
    }, {
      "refURL": "/unifiedconfig/config/agent/5201",
      "agentId": "1320",
      "firstName": "Bruce",
      "lastName": "Wayne",
      "userName": "bwayne"
    }, {
      "refURL": "/unifiedconfig/config/agent/5202",
      "agentId": "1321",
      "firstName": "Peter",
      "lastName": "Parker",
      "userName": "pparker"
    }, {
      "refURL": "/unifiedconfig/config/agent/5203",
      "agentId": "1322",
      "firstName": "Tony",
      "lastName": "Stark",
      "userName": "tstark"
    }, {
      "refURL": "/unifiedconfig/config/agent/5204",
      "agentId": "1323",
      "firstName": "Steve",
      "lastName": "Rogers",
      "userName": "srogers"
    }, {
      "refURL": "/unifiedconfig/config/agent/5205",
      "agentId": "1324",
      "firstName": "Bruce",
      "lastName": "Banner",
      "userName": "bbanner"
    }, {
      "refURL": "/unifiedconfig/config/agent/5231",
      "agentId": "1086",
      "firstName": "Annika",
      "lastName": "Hamilton",
      "userName": "annika"
    }, {
      "refURL": "/unifiedconfig/config/agent/5118",
      "agentId": "1317",
      "firstName": "Donald",
      "lastName": "Duckling",
      "userName": "dduck"
    }]
  }],
  "supervisors": [{
    "supervisor": [{
      "refURL": "/unifiedconfig/config/agent/5174",
      "agentId": "1082",
      "firstName": "Rick",
      "lastName": "Barrows",
      "userName": "rbarrows@dcloud.cisco.com"
    }]
  }]
}
Object.keys(myjsonobj).forEach(function(key) {
  if (myjsonobj[key] === value) {
    delete myjsonobj[key];
  }
});
console.log(JSON.stringify(myjsonobj));
v8wbuo2f

v8wbuo2f1#

这应该可以达到目的:

var myjsonobj = {"refURL": "/unifiedconfig/config/agentteam/5022","changeStamp": 12,"agentCount": 7,"description": "Cumulus Outbound Team","name": "CumulusOutbound","peripheral": {"id": 5000,"name": "CUCM_PG_1"},"peripheralId": 5000,"supervisorCount": 1,"agents": [{"agent": [{"refURL": "/unifiedconfig/config/agent/5197","agentId": "1085","firstName": "Owen","lastName": "Harvey","userName": "oharvey"}, {"refURL": "/unifiedconfig/config/agent/5201","agentId": "1320","firstName": "Bruce","lastName": "Wayne","userName": "bwayne"}, {"refURL": "/unifiedconfig/config/agent/5202","agentId": "1321","firstName": "Peter","lastName": "Parker","userName": "pparker"}, {"refURL": "/unifiedconfig/config/agent/5203","agentId": "1322","firstName": "Tony","lastName": "Stark","userName": "tstark"}, {"refURL": "/unifiedconfig/config/agent/5204","agentId": "1323","firstName": "Steve","lastName": "Rogers","userName": "srogers"}, {"refURL": "/unifiedconfig/config/agent/5205","agentId": "1324","firstName": "Bruce","lastName": "Banner","userName": "bbanner"}, {"refURL": "/unifiedconfig/config/agent/5231","agentId": "1086","firstName": "Annika","lastName": "Hamilton","userName": "annika"}, {"refURL": "/unifiedconfig/config/agent/5118","agentId": "1317","firstName": "Donald","lastName": "Duckling","userName": "dduck"}]}],"supervisors": [{"supervisor": [{"refURL": "/unifiedconfig/config/agent/5174","agentId": "1082","firstName": "Rick","lastName": "Barrows","userName": "rbarrows@dcloud.cisco.com"}]}]}

let value = "/unifiedconfig/config/agent/5197";
myjsonobj.agents[0].agent=myjsonobj.agents[0].agent.filter(a=>a.refURL!=value);
console.log(myjsonobj)

从您的数据来看,很明显目标值只能在agent-元素的refURL属性中找到,这就是我在上面脚本中关注的内容。

相关问题