How to Remove undefined Value from Array in JQuery?

By Hardik Savani February 5, 2023 Category : Javascript jQuery

If you have a jquery or javascript array with lots of undefined values and you want to remove all undefined values from the array. you can remove undefined items from your array without using each loop you have to just use a filter, I would like to give the example of how to remove an undefined element from a javascript array. so, let's see the below example.

Example 1:

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<meta name="viewport" content="width=device-width, initial-scale=1">

<title></title>

</head>

<body>

<script type="text/javascript">

var obj = ['3','3','5',undefined,'hi'];

obj = obj.filter(function(e){return e});

console.log(obj);

</script>

</body>

</html>

I hope it can help you...

Tags :
Shares