【发布时间】:2020-07-13 17:30:34
【问题描述】:
我正在使用 Angularjs 进行搜索,我想通过巧合的方式显示结果,例如 google 搜索,但目前我只获取 dataSource 上的值并且它没有排序,我有这个过滤器代码:
var datoFiltro = this.filter;
var dataFull = this.data;
var filteredTitle = dataFull.filter(function(item) {
if (datoFiltro && datoFiltro.length) {
var words = datoFiltro.split(" ").filter(function(word) {
return word.length >= 3;
});
var pattern = "(?<=^|\\s)(" + words.join("|") + ")";
var re = new RegExp(pattern, "gi");
return re.test(item.titulo);
}
return true;
});
var filteredCat = dataFull.filter(function(item) {
if (datoFiltro && datoFiltro.length) {
var words = datoFiltro.split(" ").filter(function(word) {
return word.length >= 3;
});
var pattern = "(?<=^|\\s)(" + words.join("|") + ")";
var re = new RegExp(pattern, "gi");
return re.test(item.nombreCategoria);
}
return true;
});
var filteredDesc = dataFull.filter(function(item) {
if (datoFiltro && datoFiltro.length) {
var words = datoFiltro.split(" ").filter(function(word) {
return word.length >= 3;
});
var pattern = "(?<=^|\\s)(" + words.join("|") + ")";
var re = new RegExp(pattern, "gi");
return re.test(item.descripcion);
}
return true;
});
var resultado = filteredCat.concat(filteredTitle, filteredDesc);
var q = [...new Map(resultado.map(obj => [JSON.stringify(obj), obj])).values()];
return q;
尽管如此,使用这个数据示例搜索得很好:
dataFull =[
{
id: 721,
titulo: "Cotizador Gastos Médicos Bx+ 2019",
descripcion: "Cotizador Gastos Médicos Bx+ Tarifas 2019",
updateDate: "2020-03-25 14:30:22.0",
idCategoria: "1",
},
{
id: 88,
titulo: "Cotizador GMM Colectivo",
descripcion: "Cotizador GMM Colectivo",
updateDate: "2020-03-25 14:27:43.0",
idCategoria: "1",
},
{
id: 302,
titulo: "Cotizador AP Escolar",
descripcion: "Cotizador Accidentes Personales Escolar",
updateDate: "2020-03-25 14:26:48.0",
idCategoria: "1",
},
{
id: 865,
titulo: "Cotizador Únikuz Bx+",
descripcion: "Cotizador Únikuz Bx+",
updateDate: "2020-03-19 13:14:01.0",
idCategoria: "1",
},
{
id: 381,
titulo: "Cotizador Premia Bx+",
descripcion: "Cotizador Premia Bx+",
updateDate: "2020-01-02 12:27:43.0",
idCategoria: "1",
},
{
id: 89,
titulo: "Cotizador Vida Grupo",
descripcion: "Cotizador Vida Grupo",
updateDate: "2019-12-26 17:20:00.0",
idCategoria: "1",
},
];
如果我搜索“bx+ únikuz”,它会返回 3 行,但我的目标(2 个巧合)在第 2 行:
我怎样才能通过巧合对这些结果进行排序?
更新:
我将代码更改为:
getData: function() {
var datoFiltro = this.filter;
var dataFull = this.data;
var query1 = $filter('filter')(this.data, this.filter);
var acumuladoQuery = [];
if (query1.length > 0) {
} else {
var palabras = datoFiltro.split(/\s+/).filter(function(word) {
return word.length >= 3;
});
for (var x = 0; x < dataFull.length; x++) {
var conteo = 0;
for (var y = 0; y < palabras.length; y++) {
var x1 = dataFull[x];
var y1 = palabras[y];
if (dataFull[x].nombreCategoria.toUpperCase().includes(palabras[y].toUpperCase()) ||
dataFull[x].titulo.toUpperCase().includes(palabras[y].toUpperCase()) ||
dataFull[x].descripcion.toUpperCase().includes(palabras[y].toUpperCase())) {
acumuladoQuery.push(dataFull[x]);
}
if (conteo == palabras.length) {
return acumuladoQuery;
}
}
}
}
var resultado = query1.concat(acumuladoQuery);
var q = [...new Map(resultado.map(obj => [JSON.stringify(obj), obj])).values()];
return q;
}
现在我得到一个出现次数的数组列表,我该如何对其进行排序?例如:
q = [
{Array1},
{Array3},
{Array3},
{Array3},
{Array2},
{Array2}
];
我想拥有:
q1 = [
{Array3},
{Array2},
{Array1}
];
【问题讨论】:
标签: javascript html angularjs sorting arraylist