【问题标题】:Order arraylist search by coincidence巧合排序arraylist搜索
【发布时间】: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


    【解决方案1】:

    我已经解决了这个问题,我分享它也许有人想做一些类似的事情:

    我的最终代码是:

    getData: function () {
                    var datoFiltro = this.filter;
                    var dataFull = this.data;
                    var query1 = $filter('filter')(this.data, this.filter);
                    var acumuladoQuery =[];
                    if (query1.length>0){
                        acumuladoQuery=query1;
                    } 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 = acumuladoQuery;
                    let count = resultado.reduce((res, val) => {
                          if(res[val.id]) {
                            res[val.id]++;
                          } else {
                            res[val.id] = 1;
                          }
                          return res;
                        }, {});             
                    let output= Object.entries(count)
                      .sort((a, b) => b[1]-a[1]) 
                      .map(function(obj){
                        var rObj = [];
                        for (var x=0; x<resultado.length; x++){                     
                            if(resultado[x].id==obj[0]){
                                if(rObj.includes(resultado[x])){                                
                                } else{
                                    rObj.push(resultado[x]);
                                }
                            }
                        }
                        return rObj;
                      }); 
                    var lista = [];
                    for (var x=0; x<output.length; x++){
                        lista[x]={};
                        lista[x]=output[x][0];
                    }
                    return lista;
                },
    

    首先我使用原始和简单的过滤器进行搜索:

    var query1 = $filter('filter')(this.data, this.filter);
    

    如果我没有任何结果,我会按每个单词进行拆分,至少 3 个字母

    if (query1.length>0){
                        acumuladoQuery=query1;
                    } 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 = acumuladoQuery;
                    let count = resultado.reduce((res, val) => {
                          if(res[val.id]) {
                            res[val.id]++;
                          } else {
                            res[val.id] = 1;
                          }
                          return res;
                        }, {});     
    

    然后我按频率排序(因为我有包含我的话的重复值,更多重复意味着可能的用户目标)一旦排序我按行获取一个对象(对象的对象)(我没有找到创建一个简单的数组)

    let output= Object.entries(count)
                      .sort((a, b) => b[1]-a[1]) 
                      .map(function(obj){
                        var rObj = [];
                        for (var x=0; x<resultado.length; x++){                     
                            if(resultado[x].id==obj[0]){
                                if(rObj.includes(resultado[x])){                                
                                } else{
                                    rObj.push(resultado[x]);
                                }
                            }
                        }
                        return rObj;
                      }); 
    

    最后我创建了一个简单的数组列表并返回它

    var lista = [];
                    for (var x=0; x<output.length; x++){
                        lista[x]={};
                        lista[x]=output[x][0];
                    }
                    return lista;
    

    我希望这段代码可能对某人有所帮助,谢谢

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-04
      • 1970-01-01
      • 2021-10-29
      • 2020-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-09
      相关资源
      最近更新 更多