使用特定ID访问属性的数组

时间:2019-02-12 14:01:50

标签: javascript

我想做的是访问随机属性,例如let1,let2,它们的第一个字符串在ID为“ 1”,“ 2”,“ 3”,“ 4”,“ 5”的数组中。 brojleta实际上是我之前提到的ID,它不同于那里的ID(变量ID = item [0] [1])。我需要的是根据其ID获取所有其他字符串。我这样尝试过:

var data = {
    let1:[["1","2","10.2.2019.","11.2.2019.","Beograd Aerodrom","Amsterdam Aerodrom","30","12000"]],
    let2:[["2","4","15.2.2019.","16.2.2019","Amsterdam Aerodrom","Rim Aerodrom","30","8000"]],
    let3:[["3","6","25.2.2019.","28.2.2019.","Rim Aerodrom","Beograd Aerodrom","30","8000"]],
    let4:[["4","8","13.2.2019.","14.2.2019.","Beograd Aerodrom","Moskva Aerodrom","30","13000"]],
    let5:[["5","10","1.3.2019.","4.3.2019.","Beograd Aerodrom","New York Aerodrom","30","18000"]]
};


function getParamValue(brojleta) {
  var location = decodeURI(window.location.toString());
  var index = location.indexOf("?") + 1;
  var subs = location.substring(index, location.length);
  var splitted = subs.split("&");

  for (i = 0; i < splitted.length; i++) {
    var s = splitted[i].split("=");
    var pName = s[0];
    var pValue = s[1];
    if (pName == brojleta) {
      return pValue;
    }
  }

}

var brojleta = getParamValue("id");
var item = data.find(item => item[0][0] === brojleta);
var id = item[0][1]
var datumpolaska = item[0][2]
var datumdolaska = item[0][3]
var polazniaerodrom = item[0][4]
var dolazniaerodrom = item[0][5]
var brojsedista = item[0][6]
var cenakarte = item[0][7]

  var data1 = data.let1[0];
  var data2 = data.let2[0];
  var data3 = data.let3[0];
  var data4 = data.let4[0];
  var data5 = data.let5[0];
  
  /* this is the code for adding data from array to table */
  
  $(document).ready(function(){
    var row1cells = $("#row1 td");
    var row2cells = $("#row2 td");
    var row3cells = $("#row3 td");
    var row4cells = $("#row4 td");
    var row5cells = $("#row5 td");
  
    for (var index=0; index<8; index++) {
      $(row1cells[index]).html(data1[index]);
      $(row2cells[index]).html(data2[index]);
      $(row3cells[index]).html(data3[index]);
      $(row4cells[index]).html(data4[index]);
      $(row5cells[index]).html(data5[index]);
    }
  });

4 个答案:

答案 0 :(得分:0)

要使代码正常工作,您应该将变量数据选择为数组数组而不是对象。然后,您可以运行var item = data.find(item => item[0] === brojleta);和类似的操作。

它看起来像这样:

var data =  [["1","2","10.2.2019.","11.2.2019.","Beograd Aerodrom","Amsterdam Aerodrom","30","12000"],
   ["2","4","15.2.2019.","16.2.2019","Amsterdam Aerodrom","Rim Aerodrom","30","8000"],
   ["3","6","25.2.2019.","28.2.2019.","Rim Aerodrom","Beograd Aerodrom","30","8000"],
    ["4","8","13.2.2019.","14.2.2019.","Beograd Aerodrom","Moskva Aerodrom","30","13000"],
    ["5","10","1.3.2019.","4.3.2019.","Beograd Aerodrom","New York Aerodrom","30","18000"]];

答案 1 :(得分:0)

我认为您真的想要这个:

测试后删除|| 3 // test #3

尝试从输入中删除3,然后单击搜索

var data = {
    let1:[["1","2","10.2.2019.","11.2.2019.","Beograd Aerodrom","Amsterdam Aerodrom","30","12000"]],
    let2:[["2","4","15.2.2019.","16.2.2019","Amsterdam Aerodrom","Rim Aerodrom","30","8000"]],
    let3:[["3","6","25.2.2019.","28.2.2019.","Rim Aerodrom","Beograd Aerodrom","30","8000"]],
    let4:[["4","8","13.2.2019.","14.2.2019.","Beograd Aerodrom","Moskva Aerodrom","30","13000"]],
    let5:[["5","10","1.3.2019.","4.3.2019.","Beograd Aerodrom","New York Aerodrom","30","18000"]]
};


function getParamValue(brojleta) {
  return new URLSearchParams(document.location.search.substring(1)).get(brojleta)
}

function show(item) {
  $tr = $("<tr/>"), $tbd = $("#tbd");
  $.each(item,function(_,fld) {
    $tr.append("<td>"+fld+"</td>");
  })
  $tr.appendTo($tbd);
}  
function showAll() {
  Object.keys(data).forEach(function(key) {
    show(data[key][0]);
  })
}  
$(function() {
  $("#search").on("click",function() {
    $("#tbd").empty();  
    var brojleta = $("#broj_leta").val();
    if (brojleta) show(data["let"+brojleta][0])
    else showAll();
  });    
  var brojleta = getParamValue("id") || 3 // test #3
  if (brojleta) $("#broj_leta").val(brojleta);
  $("#search").trigger("click");
})
th, td { border:1px solid lightgrey; padding: 3px }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" id="broj_leta" /><button id="search">Search</button>
<table>
  <thead>
    <tr>
      <th>id</th>
      <th>Datumpolaska</th>
      <th>Datumdolaska</th>
      <th>Plazniaerodrom</th>
      <th>Dolazniaerodrom</th>
      <th>Brojsedista</th>
      <th>Cenakarte</th>
  </tr>
</thead>
<tbody id="tbd">
</tbody>
</table>

答案 2 :(得分:-1)

您可以使用lodash函数find()。

此功能与Array.find相同,但适用于Object。

https://lodash.com/docs/4.17.11#find

答案 3 :(得分:-1)

您可以首先根据ID过滤数据,然后在下面的代码输出中将所需的变量映射到最终输出数组。

.input-group-append + small {
  width: 100%;
}