All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Remove Object From Array In JavaScript

Last Updated : Mar 11, 2024

How To Remove Object From Array In JavaScript

In this tutorial we will show you the solution of how to remove object from array in JavaScript, generally there are many methods with help of which we are able remove object from array.

These are array.pop(), array.filter(), array.splice(), array.shift(), array.slice(). Whereas shift is used to remove array from beginning and pop is used to remove array from last and rest are used for specific index.

Step By Step Guide On How To Remove Object From Array In JavaScript :-

As, we are going to understand splice(), shift() and slice() in these couple of articles.

In this article, first we understand splice() method. So, let us begun with codes. The general syntax of splice() is given as - Array.splice(index, count, items1, ... , itemX)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title> How to remove object from array in JavaScript </title>
</head>
<body>
<script>
var array = [{id:1, name:'Robert'},{id:2, name:'Thanos'},{id:3, name:'Dicky'}];
array.splice(0,1)
console.log(array)
</script>
</body>
</html>
  1. First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. As above now <head> tag is used to contain information about web page. In this tag a <title> tag is used which helps us to specify a webpage title. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  4. Here, then we create a body tag. All the content which we want to show on browser’s screen or display is always written inside this codes.
  5. Here, before starting let us understand syntax of splice() first. In splice there is index, count, items1, itemX. The index specifies to the integer or say position where we want to remove or delete array. Rest are like count, items1, itemX are optional here.
  6. Now, here inside script tag we write JavaScript code. As you see there we create an array named array using var. After that we store value inside this under id and name. We store three arrays there.
  7. After that we to remove array we use splice on array and use (0,1) inside splice. It means that index is 0 there are count is 1.
  8. So, we conclude that the remove or say deletion start from first index and number of elements which we want to remove are 1. Hence, in output id 1 having name Robert must be removed from there.
  9. After that we use console to print or display output. You can see output by inspecting them and by opening console tab.
  10. At last, the <body> and <html> tags are closed with </body> and </html> respectively.

Conclusion :-

At last in conclusion, here we can say that with the help of this article we are able to understand how to remove object from array in JavaScript.

I hope this tutorial on how to remove object from array in JavaScript helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪