简体   繁体   English

如何对通过在 javascript 中加入两个不同的 arrays 创建的数组进行排序

[英]How to sort an array which is created by joining two different arrays in javascript

I am creating an array of results which is formed by joining the arrays from two different requests.我正在创建一个结果数组,它是通过从两个不同的请求加入 arrays 形成的。 The results have "Places" and "Addresses".结果有“地点”和“地址”。 I am separating "Places" and "Addresses" and showing them up but I wanted to display places first and then address.我将“地点”和“地址”分开并显示它们,但我想先显示地点然后显示地址。 Can any one please help.任何人都可以帮忙。 below is my code -下面是我的代码 -

        let results_html = [];

        results_places = ['<li style="margin: 12px"><i style="display:block;float:left" class="material-icons">place</i><span style="font-size:1.25em;font-size: 20px">Places</span></li>']
        results_addresses = ['<li style="margin: 12px"><i style="display:block;float:left;font-size: 20px" class="material-icons">home</i><span style="font-size:1.25em">Addresses</span></li>']

        if (predictions && predictions.length) {
          // debugger;
          if (predictions[0].types.join().indexOf("postal_code")!=-1 || predictions[0].types.join().indexOf("locality")!=-1) {

            results_html.push(results_places);

          } else if(predictions[0].types.join().indexOf("health")!=-1|| predictions[0].types.join().indexOf("hospital")!=-1 || predictions[0].types.join().indexOf("route")!=-1 || predictions[0].types.join().indexOf("premise")!=-1 || predictions[0].types.join().indexOf("street_address")!=-1) {

            results_html.push(results_addresses);

          }

          predictions.forEach(function (prediction) {
            results_html.push(`<li class="autocomplete-item" data-type="place" data-place-id=${prediction.place_id}>      
                                    <span class="autocomplete-text">${prediction.description}</span></li>`);


          });
        }

        if (predictions2) {

          if (predictions2[0].types.join().indexOf("postal_code")!=-1 || predictions2[0].types.join().indexOf("locality")!=-1) {

            results_html.push(results_places);

          } else if(predictions2[0].types.join().indexOf("health")!=-1|| predictions2[0].types.join().indexOf("hospital")!=-1 || predictions2[0].types.join().indexOf("route")!=-1 || predictions2[0].types.join().indexOf("premise")!=-1 || predictions2[0].types.join().indexOf("street_address")!=-1) {

            results_html.push(results_addresses);

          }

          predictions2.forEach(function (prediction) {
            results_html.push(`<li class="autocomplete-item" data-type="place" data-place-id=${prediction.place_id}>      
                                    <span class="autocomplete-text">${prediction.description}</span></li>`);
           });
        }

        autocomplete_results.innerHTML = results_html.join("");

Here is the current output -这是当前的 output -

在此处输入图像描述

I have looked into your code just edit this line to我已经查看了您的代码,只需将此行编辑为

let displaySuggestions = function (predictions2, predictions) 

here is jsfiddle这是jsfiddle

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM