简体   繁体   中英

Delete property of an array object using javascript

I have an array object in the below format and need to delete a property of an array.

Code:

var partDetailArray={[PartId:100,PartRowID:'row-1']
,[PartId:100,PartRowID:'row-1']};
delete partDetailsArray.PartRowID;

I need to remove the PartRowID property from the array, but the delete statement isn't working.

If you look in the console, you'll see that you're getting syntax errors with that code for the reasons I put in my comment on the question.

I'm going to guess you meant to have an array containing objects, like this:

var partDetailArray = [
    {PartId:100,PartRowID:'row-1'},
    {PartId:100,PartRowID:'row-1'}
];

In that case, the array doesn't have a PartRowID property, but each of the objects in it does. So we need to index into the array to access the object, and delete the property from it:

delete partDetailsArray[0].PartRowID;
// Indexing into it ---^^^

That would delete that property from the first one. If you want to delete all of them, you'll need a loop:

var index;
for (index = 0; index < partDetailArray.length; ++index) {
    delete partDetailsArray[index].PartRowID;
}

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