0

I am creating array object like follows:

var numberOfSameDeficiency = [];
 for (var i = 0; i < result.length; i++) {

                    var deficiencyId = result[i].Deficiency_Id;
                    var deficiencyName = result[i].DeficiencyName;

                    //check to see if this deficiency is already in the list of available selections
                    if ($("#drpDeficiency option[value='" + deficiencyId + "']").length == 0) {
                        var option = $('<option>');
                        option.attr('value', deficiencyId);
                        option.text(deficiencyName);

                        $select.append(option);
                    }
                    else {
                        Tests = {};
                        Tests.TestId = testId;
                        Tests.DeficiencyId = deficiencyId;
                        numberOfSameDeficiency.push(Tests);
                    }
                }

And I want to remove object on different function like this:

for (var i = 0; i < result.length; i++) {
                console.log(numberOfSameDeficiency);
                var isFound = false;
                var deficiencyId = result[i].Deficiency_Id;

                if (numberOfSameDeficiency) {

                    numberOfSameDeficiency.forEach(function (entry) {
                       if (entry.DeficiencyId != deficiencyId) {
                           isFound = true;
                        **numberOfSameDeficiency.splice(entry, 1); // Generating Error (Remove all items from array object)** 
                           return;
                       }

                    });
                    // console.log("end if");
                }
                if (!isFound) {
                    $("#drpDeficiency option[value='" + deficiencyId + "']").remove();
                }

            }

So what line code should be there to remove particular object from array object.

2 Answers 2

2

Try this

for( i=myArray.length-1; i>=0; i--) {
    if( myArray[i].field == "money") myArray.splice(i,1);
}

This also works

myArray = [{name:"Alpesh", lines:"2,5,10"},
             {name:"Krunal", lines:"1,19,26,96"},
             {name:"Deep",lines:"3,9,62,36" }]
johnRemovedArray = myArray
               .filter(function (el) {
                        return el.name !== "Krunal";
                       });
Sign up to request clarification or add additional context in comments.

1 Comment

I think this is right. In your code you are affecting the original list you are calling forEach on - this is not good. for loop doesn't care about the contents of the array, just length
0

Create this prototype function:

Array.prototype.removeElement = function (el) {
    for(let i in this){
        if(this.hasOwnProperty(i))
            if(this[i] === el)
                this.splice(i, 1);
    }
}

Then call:

let myArray = ['a','b','c','d'];
myArray.removeElement("c");

It also works with objects:

let myObj1 = {name: "Mike"},
    myObj2 = {name: "Jenny"},
    myArray = [myObj1, myObj2];

myArray.removeElement(myObj2);

Comments

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.