简体   繁体   中英

Removing matched object from array of objects using javascript

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

在此处输入图片说明

在此处输入图片说明

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM