1

I have an array of 4 objects and each object contains property array of 8 object.

enter image description here

enter image description here

I am trying to remove an object from properties Array[8]

        var responseArray = new Array();
        responseArray = response.data;
         responseArray.forEach(function (resProp) {
            if (resProp.alias == "General Details") {
                resProp.properties.forEach(function (checkProp) {

                    if (checkProp.alias == "name") {
                        responseArray.pop(checkProp);
                    }
                });
            }

        });

I am able to pop it however the responseArray having only 3 object Array instead of 4.i think, this code is removing the whole 4th object.

responseArray.pop(checkProp);

any suggestions on removing only matched object?

2
  • please add the object in text form. Commented Apr 6, 2016 at 9:15
  • see docs Array.prototype.pop Commented Apr 6, 2016 at 9:15

1 Answer 1

2
  • Pop method is not suitable to remove specific object from an array

The pop() method removes the last element from an array and returns that element.

  • You should remove object from responseArray.properties array instead of responseArray

Replace responseArray.pop(checkProp); with resProp.properties.splice( resProp.properties.indexOf(checkProp) , 1 );

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

4 Comments

i tried its not working.i think it need to splice from responseArray as its the only which is defined however getting "indexOf is not defined"
@Harshit Oh, my bad, sure you would get that error, indexOf does not have a context. I updated my answer try it now.
great.thanks a lot :) its working.just concern on the compatibility of indexOf operator.is there a other way to get the index?
good point, indexOf is supported from IE9 +, if that is still matter , you could iterate thru resProp.properties using for loop instead of forEach and for current element (for which you have index too) pass that index to splice method as a first parameter.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.