简体   繁体   中英

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.

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 != .

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!!!!

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