我正在使用jQuery.unique()摆脱Javascript数组内的double值。在Chrome中,效果很好,数组的长度从60到29;在Firefox中,它的效果很奇怪,从60到43;在safari中,它根本不起作用,有什么提示吗?
这是我使用的导入:
<title>APS</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<link href="../css/jquery-ui-1.10.4.css" rel="stylesheet">
<link rel="stylesheet" href="../css/reset.css">
<link href="http://s3.amazonaws.com/codecademy-content/courses/ltp/css/shift.css" rel="stylesheet">
<link rel="stylesheet" href="http://s3.amazonaws.com/codecademy-content/courses/ltp/css/bootstrap.css">
<script src="../js/jquery-1.10.2.js"></script>
<script src="../js/jquery-ui-1.10.4.js"></script>
<script src = "../js/search.js"></script>
<link rel="stylesheet" href="../css/styles.css">
<script type="text/javascript" src="http://www.parsecdn.com/js/parse-1.2.18.min.js"></script>
</head>
和js:
$(function() {
Parse.initialize("T1S9kR0SEibdUjxpyOzaYccrGMVJmwOoaqclBDDO", "IiGFcSejnTIvfhTIEumgElGx9vru9MbdMora8SSZ");
var query = new Parse.Query("pruebas");
query.find({
success: function(objects) {
var centros=["VILLAVICIOSA", "EL VALLE", "PALOMERA", "ENCINAR", "SAN MATEO"];
var tipos = ["Esporadico", "Semanal", "Esperando"];
var nombres=[];
var pruebas = ["Nacho", "Nacho ", "NACHO", "Nacho"];
console.log(pruebas.length+pruebas);
console.log(jQuery.unique(pruebas).length+jQuery.unique(pruebas));
var emails=[];
var dias = [];//"Lunes", "Martes", "Miercoles", "Jueves", "Viernes", "Sabado", "Domingo"];
var dnis=[];
var i =0;
objects.forEach(function(obj) {
nombres[i] = obj.attributes.NOMBRE;
emails[i] = obj.attributes.MAIL;
dnis[i] = obj.attributes.DNI;
dias[i] = obj.attributes.DIA;
i++;
});
console.log("longitud de dias antes: "+dias.length);
console.log("longitud de dias despues: "+jQuery.unique(dias).length);
$('#nombres').focus();
$( "#nombres" ).autocomplete({
select: function( event, ui ) {console.log(ui.item.value);$('#emails').val('');$('#dnis').val('');
$('#dias').val('');$('#centros').val('');$('#tipos').val('');},
source: nombres,
autoFocus: true
});
$( "#dias" ).autocomplete({
select: function( event, ui ) {
console.log(ui.item.value);
console.log(
objects.filter(function( obj ) {
return obj.attributes.DIA == ui.item.value;
}));
$('#emails').val('');$('#dnis').val('');
$('#nombres').val('');},
source: jQuery.unique( dias ),
autoFocus: true
});
$( "#emails" ).autocomplete({
select: function( event, ui ) {console.log(ui.item.value);$('#centros').val('');$('#nombres').val('');$('#dnis').val('');
$('#dias').val('');$('#tipos').val('');},
source: emails,
autoFocus: true
});
$( "#dnis" ).autocomplete({
select: function( event, ui ) {console.log(ui.item.value);$('#centros').val('');$('#emails').val('');$('#nombres').val('');
$('#dias').val('');$('#tipos').val('');},
source: dnis,
autoFocus: true
});
$( "#centros" ).autocomplete({
select: function( event, ui ) {console.log(ui.item.value);$('#emails').val('');$('#nombres').val('');},
source: centros,
autoFocus: true
});
$( "#tipos" ).autocomplete({
select: function( event, ui ) {console.log(ui.item.value);$('#emails').val('');$('#nombres').val('');},
source: tipos,
autoFocus: true
});
},
error: function(error) {
console.log("An error occured :(");
}
});
$('#borrarBusq').click(function(){
$('#nombres').val('');
$('#emails').val('');
$('#dnis').val('');
$('#dias').val('');
$('#tipos').val('');
$('#centros').val('');
});
});
谢谢!!
最佳答案
jQuery.unique仅适用于DOM元素:
描述:对DOM元素数组进行适当排序,并删除重复项。请注意,这仅适用于DOM元素数组,不适用于字符串或数字。
您需要实现自己的例程或使用诸如lodash或underscore之类的东西
关于javascript - jQuery在Firefox上无法正常运行,并且在Safari中根本无法正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23712462/