ashwin
ashwin

Reputation: 99

How to filter single data from array using javascript/jquery?

var auditorListValue = ["", "1", "2", "3", "4"];
var oldAuditGroupId = 3;
var auditorListValue = auditorListValue.filter(function (item) {
	return item !== oldAuditGroupId;
});
console.log(auditorListValue);
<!DOCTYPE html>
<html>
<head>
	<title></title>
</head>
<body>

</body>


</html>

I want to remove this "3" from array and push remaining data into array but it is not working. I have tried this same method for other array but it is working,but it is not working for this array.

Upvotes: 0

Views: 35

Answers (2)

luissmg
luissmg

Reputation: 580

It is because in your original array you have strings and you are comparing strings to a number using !==. IT will always return false unless both of the items have the same type or you compare using !=.

Upvotes: 1

iamrajshah
iamrajshah

Reputation: 979

var auditorListValue = ["", "1", "2", "3", "4"];
var oldAuditGroupId = "3";
var auditorListValue = auditorListValue.filter(function (item) {
    return item !== oldAuditGroupId;
});
console.log(auditorListValue);

This will work!!! You are filtering integer instead of String.

I hope you understand!!!!

Upvotes: 0

Related Questions