简体   繁体   English

如何使用javascript / jquery从数组中过滤单个数据?

[英]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. 我想从数组中删除此“ 3”并将剩余数据推入数组,但是它不起作用。 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 != . 除非两个项目的类型相同或您使用!=进行比较,否则IT始终将返回false

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!!!! 我希望你明白!!!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用javascript / jquery从数组中过滤数据? - How to filter the data from array using javascript/jquery? 如何从javascript中的数组中过滤数据 - How to filter data from array in javascript 如何使用Javascript / JQuery从Array中的JsonArray获取数据? - How to get data from JsonArray within Array using Javascript/JQuery? 如何使用同位素使用Javascript从数组中过滤单个随机图像 - How to use Isotope to filter a single random image with Javascript from an array 如何在jquery javascript中过滤数组 - how to filter array in jquery javascript 使用 jQuery 或 JavaScript 从数组中提取数据 - Extract the data from array using jQuery or JavaScript 如何使用javascript,jquery或其他一些javascript框架过滤数据? - How to filter data using javascript, jquery, or some other javascript framework? 如何使用来自不同一维数组的数据过滤二维数组 - Javascript/Google Apps 脚本? - How to filter a two dimensional array using data from a different one dimensional array - Javascript/Google Apps Script? JavaScript从数组过滤数据 - JavaScript filter data from array 如何使用条件过滤数组并在 JavaScript 中获取具有首选数据的数组? - How to filter an array by using conditions and get the array with preferred data in JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM