How do I remove an object from an array with JavaScript

0 votes

I have a JavaScript object that looks like this:

id="1";
name = "serdar";

I also have an Array that contains several of the above things. 

How do I delete an item from an array like this:

obj[1].remove();
Dec 8, 2022 in Java by Nicholas
• 7,760 points
• 1,227 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

Splice is effective:

var arr = [{id:1,name:'serdar'}];
arr.splice(0,1);
// []

Arrays should not be deleted using the delete operator. 

delete does not remove an entry from an Array; instead, it replaces it with undefined.

var arr = [0,1,2];
delete arr[1];
// [0, undefined, 2]

But perhaps you'd like something like this?

var removeByAttr = function(arr, attr, value){
    var i = arr.length;
    while(i--){
       if( arr[i] 
           && arr[i].hasOwnProperty(attr) 
           && (arguments.length > 2 && arr[i][attr] === value ) ){ 

           arr.splice(i,1);

       }
    }
    return arr;
}
answered Dec 10, 2022 by Damonlang
• 1,230 points

edited Mar 5, 2025

Related Questions In Java

0 votes
1 answer

How to remove object from an Array?

Here is a code I came up ...READ MORE

answered Mar 11, 2019 in Java by Esha Gupta
• 1,619 views
0 votes
0 answers

How can I remove a specific item from an array?

How do I get rid of a specific value from an array?  As an example: array.remove(value); // removes all elements with ...READ MORE

Sep 21, 2022 in Java by Nicholas
• 7,760 points
• 1,461 views
0 votes
0 answers

How do I check if an array includes a value in JavaScript?

What is the shortest and most efficient ...READ MORE

Sep 28, 2022 in Java by Nicholas
• 7,760 points
• 1,432 views
0 votes
2 answers

How do I read and convert an InputStream object to string type?

You can also use Java Standard Library ...READ MORE

answered Jul 17, 2018 in Java by Sushmita
• 6,920 points
• 19,503 views
0 votes
2 answers

How do I convert an input stream into a byte array

You could probably try this if you ...READ MORE

answered Aug 29, 2019 in Java by Sirajul
• 59,190 points
• 5,494 views
0 votes
2 answers

How can we remove an element from an array in Java?

You can use ArrayUtils class remove method ...READ MORE

answered May 24, 2018 in Java by UshaK
• 4,581 views
0 votes
0 answers

Convert Array to Object

How can I convert this : ['a','b','c'] To this: { ...READ MORE

May 19, 2022 in Java-Script by Kichu
• 19,040 points
• 1,533 views
0 votes
0 answers

Convert Array to Object

What is the best way to convert: ['a','b','c'] to: { ...READ MORE

Sep 20, 2022 in Java by Nicholas
• 7,760 points
• 1,577 views
0 votes
0 answers

What is the difference between ( for... in ) and ( for... of ) statements?

I'm familiar with a for... in loop (it iterates over the keys), but this is the first I've heard of a for... of loop (it iterates over values). I don't understand the for...of loop. var arr = [3, 5, 7]; arr.foo ...READ MORE

Nov 9, 2022 in Java by Nicholas
• 7,760 points
• 1,321 views
0 votes
1 answer

How to store an array in localstorage?

Localstorage only supports Strings. So you can ...READ MORE

answered Jul 1, 2019 in Others by sunshine
• 1,300 points
• 24,964 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP