在javascript中多次过滤

时间:2013-08-02 05:50:43

标签: javascript object associative-array

所以,我有一个名为products的对象有3个属性:

var products = [
  {"name":"product1","size":"large","color":"blue","gender":"male"},
  {"name":"product2","size":"small","color":"pink","gender":"female"},
  {"name":"product3","size":"large","color":"green","gender":"male"},
  {"name":"product4","size":"large","color":"yellow","gender":"female"},
  {"name":"product5","size":"medium","color":"blue","gender":"female"},
  {"name":"product6","size":"large","color":"green","gender":"male"},
  {"name":"product7","size":"small","color":"yellow","gender":"male"},
  {"name":"product8","size":"medium","color":"red","gender":"female"},
  {"name":"product9","size":"large","color":"blue","gender":"male"},
  {"name":"product10","size":"small","color":"red","gender":"female"}
];

因此,如果大小,颜色和性别 3个选择框,我如何过滤这3个以获取产品名称?

我正在尝试在javascript中使用.filter。我知道如何在非关联数组中使用它。但是,关联数组怎么样?你怎么用它们?

3 个答案:

答案 0 :(得分:3)

var color = document.getElementById("color").value;
var gender = document.getElementById("gender").value;
var size = document.getElementById("size").value;
var matched = products.filter(function(e) {
    return (e.color == color && e.gender == gender && e.size == size);
}).map(function(e) { return e.name; });

答案 1 :(得分:1)

我写了一个JSfiddle来回答这个问题。请在此处查看:http://jsfiddle.net/THEtheChad/XjGPt/

JQuery的

$('input').change(function(){
   var names = filter();
});

function filter(){
  var selected = {
    size:   $('#size')  .val(),
    color:  $('#color') .val(),
    gender: $('#gender').val()

  };

  var matches = products.filter(function(product){
    return product.size == selected.size   &&
           product.color == selected.color && 
           product.gender == selected.gender;
  });

  return matches.map(function(product){ return product.name });
}

Vanilla JS

var d = document;

var inputs = d.getElementsByTagName('input');
// Convert to array
inputs = Array.prototype.slice.call(inputs);

inputs.forEach(function(input){
   input.addEventListener('change', function(e){
      var names = filter();
   });
});

function filter(){
  var selected = {
    size:   d.getElementById('size')  .value,
    color:  d.getElementById('color') .value,
    gender: d.getElementById('gender').value

  };

  var matches = products.filter(function(product){
    return product.size == selected.size   &&
           product.color == selected.color && 
           product.gender == selected.gender;
  });

  return matches.map(function(product){ return product.name });
}

答案 2 :(得分:1)

不像Barmar的代码那么优雅,我实现了3个dropbox中的2个,并为你留下了一些工作;)

<html>
<head>
    <script>
var products = [
  {"name":"product1","size":"large","color":"blue","gender":"male"},
  {"name":"product2","size":"small","color":"pink","gender":"female"},
  {"name":"product3","size":"large","color":"green","gender":"male"},
  {"name":"product4","size":"large","color":"yellow","gender":"female"},
  {"name":"product5","size":"medium","color":"blue","gender":"female"},
  {"name":"product6","size":"large","color":"green","gender":"male"},
  {"name":"product7","size":"small","color":"yellow","gender":"male"},
  {"name":"product8","size":"medium","color":"red","gender":"female"},
  {"name":"product9","size":"large","color":"blue","gender":"male"},
  {"name":"product10","size":"small","color":"red","gender":"female"}
];

function checkValidOption(){
    var color_chosen = document.getElementById("color").value;
    var size_chosen = document.getElementById("size").value;
    var result = "";
    //only if both options were chosen 
    if (color_chosen !== "empty" && size_chosen != "empty"){        
        for(var i=0; i<products.length; i++){
            if(products[i].size == size_chosen && products[i].color == color_chosen){
                result = products[i].name;
                break;
            }
        }
        document.getElementById('result').innerHTML = result;
    }    
}

    </script>   
</head>

<body>
    <div id="wrapper">
        <select id="size" name="size" onchange="checkValidOption();">
            <option value="empty"/>         
            <option value="small">small</option>
            <option value="medium">medium</option>
            <option value="large">large</option>
        </select>
        <select id="color" name="color" onchange="checkValidOption();">
            <option value="empty"/>         
            <option value="red">red</option>
            <option value="yellow">yellow</option>
            <option value="blue">blue</option>
            <option value="green">green</option>
            <option value="pink">pink</option>
        </select>
    </div><!--wrapper-->
    <div id="result"></div>

    </body>
</html>