0

I have an object and I want to remove the element from it.

Eg : My object is like as below

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":""}]}';

Suppose I want to delete row with recid = 662

Below is the code i tried

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

But the above code gives me an error like TypeError: obj.Decision[e] is undefined on line 21.

How shall I solve this.Help appreciated!Thanks!

1
  • Use instead for(properties in obj.Decision) and access DRecVal by properties.recid Commented Apr 15, 2015 at 15:20

4 Answers 4

2

Just add a

break;

after the splice() call in the loop.

Sign up to request clarification or add additional context in comments.

1 Comment

Rhinodevil , You are a genius man!You solved my query in such a short line of code.Thanks a lot
1

'.length' is not zero indexed. Ammend your variable to the following:

var ddcnt = obj.Decision.length - 1;

3 Comments

you get undefined, because the loop is looking for a 4th item at index 3 in the array.
yes, becuase .length returns a one-indexed value and arrays are zero-indexed
length isn't indexed, as such; it's an absolute value (whether the array starts at 0 or 1, it still has N items).
0

This works accepting the recid string:

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

Comments

0

Try this

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

UPDATE :

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

8 Comments

Thanks Divakar.I tried this code but then I am not able to remove the last element here.Here the last element is 752.Any suggestion?
@MuktaChourishi Are you not trying to remove the last element? Your question says that you need to remove a row with recid 662. The above code removes row with recid 662 as stated in your question.
That's an example.I would get recid dynamically and it may happen that 752 is to be deleted.
@MuktaChourishi then you can put this into a function and pass the recid to be deleted as a parameter. Check my updated answer.
Divakar , Code is not working for the last element in array for others it does.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.