数组元素未显示JavaScript

时间:2018-07-18 18:56:22

标签: javascript html filter web

我目前正在尝试为节日网站创建过滤系统。我已经使用了价格过滤器,但是现在我要过滤类型。

我在类型过滤器中使用了一个名为TabWidget的数组,该数组仅包含从价格过滤器中推送的元素。每当我尝试在类型过滤器之后提醒此数组时,提醒始终为空?感谢您的任何答复。

代码:

filterfestivals
// Festival[x][0]   city[x][1]           cost[x][2]    date[x][3]                genre[x][4]
var festivals = [
  ["Reading",       "Richfield Avenue",  205,          "24th - 26th August",     "Rock"],
  ["Park Life",     "Manchester",        140,          "8th - 9th June",         "Pop"],
  ["Glastonbury",   "Somerset",          250,          "23rd - 25th June",       "Alternative"],
  ["Lovebox",       "London",            120,          "8th - 9th June",         "Pop"],
  ["Bluedot",       "Macclesfield",      175,          "19th - 22nd July",       "Alternative"],
  ["Bestival",      "Dorset",            192,          "2nd - 5th August",       "Dance"],
  ["V festival",    "Staffordshire",     140,          "18th - 19th August",     "Pop"],
  ["Latitude",      "Suffolk",           197,          "12th - 15th July",       "Dance"],
  ["Download",      "Derby",             200,          "14th - 16th June",       "Rock"],
];
var filterfestivals = [[]];    

function filter(){
  var maxuserprice = document.getElementById("maxprice").value;
  for (var i = 0; i < festivals.length; i++)
    if (maxuserprice > festivals[i][2]){
      filterfestivals.push(festivals[i])
      //i = i + 1
    }

  var usergenre = document.getElementById("festivalgenre").value;
  for (var i = 0; i < filterfestivals.length; i++)
    if (usergenre != filterfestivals[i][4]){
      filterfestivals.splice(filterfestivals[i])
    }
  alert(filterfestivals)
}

2 个答案:

答案 0 :(得分:0)

只需使用Array.prototype.filter()来过滤您的记录

var festivals = [
                ["Reading",       "Richfield Avenue",  205,          "24th - 26th August",     "Rock"],
                ["Park Life",     "Manchester",        140,          "8th - 9th June",         "Pop"],
                ["Glastonbury",   "Somerset",          250,          "23rd - 25th June",       "Alternative"],
                ["Lovebox",       "London",            120,          "8th - 9th June",         "Pop"],
                ["Bluedot",       "Macclesfield",      175,          "19th - 22nd July",       "Alternative"],
                ["Bestival",      "Dorset",            192,          "2nd - 5th August",       "Dance"],
                ["V festival",    "Staffordshire",     140,          "18th - 19th August",     "Pop"],
                ["Latitude",      "Suffolk",           197,         "12th - 15th July",       "Dance"],
                ["Download",      "Derby",             200,           "14th - 16th June",       "Rock"],
                ];


function filter(){
  var maxuserprice = document.getElementById("maxprice").value;
  var usergenre = document.getElementById("festivalgenre").value;
  var filterfestivals = festivals.filter(item => maxuserprice > item[2]);
  filterfestivals = filterfestivals.filter(item => item[4] === usergenre);
  alert(filterfestivals)
}
<p1> Please enter your maximum spending price </p1>
<input id = "maxprice"> </input>
<button onclick = "filter()"> Filter </button>
<br />
<p1> Select a genre </p1>
<select id = "festivalgenre">
        <option value = "Rock"> Rock </option>
        <option value = "Pop"> Pop </option>
        <option value = "Alternative"> Alternative </option>
        <option value = "Dance"> Dance </option>
</select>

答案 1 :(得分:0)

您使用的接头不正确。

详细了解here

def func(tuples, maxVal):
    values = [i[1] for i in tuples]
    result = [seq for s in range(len(values), 0, -1) for seq in itertools.combinations(values, s) if sum(seq) <= maxVal]
    print(result)
  

开始

     

开始更改数组的索引(原点为0)。 [...]

     

deleteCount

     

如果省略deleteCount,或者其值大于array.length-开始(即,如果它大于数组中剩余的元素数量,则从start开始),则从start开始的所有元素通过数组的末尾将被删除。

或者您也可以按照@Zohaib Ijaz所述使用filter方法