我有一个对象,我想从中删除元素。
例如:我的对象如下所示
var DecisionObj = '{"Decision":[{"recid":"1183","reason":"Approved but with different Funding Amount","decision":"Approved","approvalamt":"","comment":""},{"recid":"662","reason":"Approved but with different Funding Amount","decision":"Approved","approvalamt":"","comment":""},{"recid":"752","reason":"Approved but with different Funding Amount","decision":"Approved","approvalamt":"","comment":""}]}';
假设我想删除recid = 662
的行以下是我试过的代码
var DecisionObj = '{"Decision":[{"recid":"1183","reason":"Approved but with different Funding Amount","decision":"Approved","approvalamt":"","comment":""},{"recid":"662","reason":"Approved but with different Funding Amount","decision":"Approved","approvalamt":"","comment":""},{"recid":"752","reason":"Approved but with different Funding Amount","decision":"Approved","approvalamt":"","comment":""}]}';
obj = JSON.parse(DecisionObj);
console.log("BEFORE==="+JSON.stringify(obj));
var ddcnt = obj.Decision.length;
var recval = 662;
for (e = 0; e < ddcnt; e++)
{
//var DRecVal = obj.Decision[e].recid;
var DRecVal = obj.Decision[e].recid;
if(recval == DRecVal)
{
obj.Decision.splice(e,1);
}
}
console.log("AFTER==="+JSON.stringify(obj));
但上面的代码给出了类似TypeError的错误: obj.Decision [e]在第21行未定义。
我该如何解决这个问题。帮助了!谢谢!
答案 0 :(得分:2)
只需添加一个
break;
在循环中调用splice()之后。
答案 1 :(得分:1)
&#39;。长度&#39;不是零索引。将您的变量修改为以下内容:
var ddcnt = obj.Decision.length - 1;
答案 2 :(得分:0)
试试这个
var DecisionObj = '{"Decision":[{"recid":"1183","reason":"Approved but with different Funding Amount","decision":"Approved","approvalamt":"","comment":""},{"recid":"662","reason":"Approved but with different Funding Amount","decision":"Approved","approvalamt":"","comment":""},{"recid":"752","reason":"Approved but with different Funding Amount","decision":"Approved","approvalamt":"","comment":""}]}';
obj = JSON.parse(DecisionObj);
console.log("BEFORE==="+JSON.stringify(obj));
var ddcnt = obj.Decision.length;
var recval = 662;
for (e = 0; e < ddcnt; e++)
{
//var DRecVal = obj.Decision[e].recid;
var DRecVal = obj.Decision[e].recid;
if(recval == DRecVal)
{
obj.Decision.splice(e,1);
break;
}
}
console.log("AFTER==="+JSON.stringify(obj));
更新:
function removeID(recid){
var DecisionObj = '{"Decision":[{"recid":"1183","reason":"Approved but with different Funding Amount","decision":"Approved","approvalamt":"","comment":""},{"recid":"662","reason":"Approved but with different Funding Amount","decision":"Approved","approvalamt":"","comment":""},{"recid":"752","reason":"Approved but with different Funding Amount","decision":"Approved","approvalamt":"","comment":""}]}';
obj = JSON.parse(DecisionObj);
console.log("BEFORE==="+JSON.stringify(obj));
var ddcnt = obj.Decision.length;
for (e = 0; e < ddcnt; e++) {
var DRecVal = obj.Decision[e].recid;
if(recid == DRecVal){
obj.Decision.splice(e,1);
}
}
console.log("AFTER==="+JSON.stringify(obj));
}
removeID(752);
答案 3 :(得分:0)
这可以接受recid字符串:
var DecisionObj = '{"Decision":[{"recid":"1183","reason":"Approved but with different Funding Amount","decision":"Approved","approvalamt":"","comment":""},{"recid":"662","reason":"Approved but with different Funding Amount","decision":"Approved","approvalamt":"","comment":""},{"recid":"752","reason":"Approved but with different Funding Amount","decision":"Approved","approvalamt":"","comment":""}]}';
var DecisionObj = JSON.parse(DecisionObj);
function removeByRecid(arr, recid) {
for (var elem in arr) {
if (arr.hasOwnProperty(elem)) {
if((typeof arr[elem].recid !== 'undefined') && (arr[elem].recid == recid))
{
console.log(arr[elem]);
var index = arr.indexOf(elem);
arr.splice(index, 1);
}
}
}
}
function removeRecid(obj, recid) {
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
if (Object.prototype.toString.call(obj[key]) === '[object Array]') {
removeByRecid(obj[key], recid);
}
}
}
}
console.log(DecisionObj);
removeRecid(DecisionObj, "1183");
console.log(DecisionObj);