过滤多个属性的

时间:2017-04-28 13:42:10

标签: javascript json

我对编码很陌生,并且遇到了以下问题。

我的JSON数据如下所示:



var data = [
	{
		"location_name": "Training Club",
		"Area Code": "C",
		"location_type": "Fitness",
		"postcode": "1094"
	},
	{
		"location_name": "Boxing Center",
		"Area Code": "C",
		"location_type": "Martial Arts",
		"postcode": "1096AN"
	},
	{
		"location_name": "Boxing Center 2",
		"Area Code": "F",
		"location_type": "Martial Arts",
		"postcode": "1096AN"
	},
	{
		"location_name": "DanceDance",
		"Area Code": "A",
		"location_type": "Dance",
		"postcode": "1093JX"
	},
	{
		"location_name": "CrossFit Box",
		"Area Code": "A",
		"location_type": "CrossFit",
		"postcode": "1019AJ"
	},
	{
		"location_name": "Gym A",
		"Area Code": "C",
		"location_type": "Fitness",
		"postcode": "1098NJ"
	}, ]//and so  on.....




我通过webhook从表单中获取输入并获得以下结果:

区号=" C" sport_type ="健身"

所以现在我想用区号' C'来搜索/过滤所有选项的数据。和健身。

这段代码只会给我列表的第一个结果:



search = JSON.search(inputData.area_code === 'C');
console.log(search);
var C = JSON.search(data[0]);

for (var i=0; i < data.length; i++) {    
    console.log(data[i]);
};
	
output = {search};
&#13;
&#13;
&#13;

有人有想法解决这个问题吗? 干杯, 尼科尔

3 个答案:

答案 0 :(得分:1)

使用过滤功能并将您的条件指定为&amp;&amp;操作数

data.filter(function(el){
         return (el["Area Code"] == "C" && el.location_type == "Fitness"); 
      });

var data = [
	{
		"location_name": "Training Club",
		"Area Code": "C",
		"location_type": "Fitness",
		"postcode": "1094"
	},
	{
		"location_name": "Boxing Center",
		"Area Code": "C",
		"location_type": "Martial Arts",
		"postcode": "1096AN"
	},
	{
		"location_name": "Boxing Center 2",
		"Area Code": "F",
		"location_type": "Martial Arts",
		"postcode": "1096AN"
	},
	{
		"location_name": "DanceDance",
		"Area Code": "A",
		"location_type": "Dance",
		"postcode": "1093JX"
	},
	{
		"location_name": "CrossFit Box",
		"Area Code": "A",
		"location_type": "CrossFit",
		"postcode": "1019AJ"
	},
	{
		"location_name": "Gym A",
		"Area Code": "C",
		"location_type": "Fitness",
		"postcode": "1098NJ"
	}]

   var s = data.filter(function(el){
     return (el["Area Code"] == "C" && el.location_type == "Fitness"); 
  });

  console.log(s);

答案 1 :(得分:0)

正如其他答案所提到的,您可以使用过滤器来删除结果。我会建议你使用三等于最佳实践:

var result = data.filter(function(item) {
    return item["Area Code"] === "C" && item["location_type"] === "Fitness";
});

更现代的语法是:

let result = data.filter(item => item["Area Code"] === "C" && item["location_type"] === "Fitness");

你也可以使用循环来做,即使性能略好一些,它也不是那么可读:

var data = [ ... ];
var result = [];
for (var i = 0; i < data.length; i++) {    
    if (data[i]["Area Code"] === "C" && data[i]["location_type"] === "Fitness") {
        result.push(data[i]);
    }
};

答案 2 :(得分:0)

<强> 1。使用ES6方法使用var data = [ { "location_name": "Training Club", "Area Code": "C", "location_type": "Fitness", "postcode": "1094" }, { "location_name": "Boxing Center", "Area Code": "C", "location_type": "Martial Arts", "postcode": "1096AN" }, { "location_name": "Boxing Center 2", "Area Code": "F", "location_type": "Martial Arts", "postcode": "1096AN" }, { "location_name": "DanceDance", "Area Code": "A", "location_type": "Dance", "postcode": "1093JX" }, { "location_name": "CrossFit Box", "Area Code": "A", "location_type": "CrossFit", "postcode": "1019AJ" }, { "location_name": "Gym A", "Area Code": "C", "location_type": "Fitness", "postcode": "1098NJ" }]; var res = data.filter(item => { return item["Area Code"] == "C" && item["location_type"] == "Fitness" }); console.log(res); 箭头功能。

var data = [
	{
		"location_name": "Training Club",
		"Area Code": "C",
		"location_type": "Fitness",
		"postcode": "1094"
	},
	{
		"location_name": "Boxing Center",
		"Area Code": "C",
		"location_type": "Martial Arts",
		"postcode": "1096AN"
	},
	{
		"location_name": "Boxing Center 2",
		"Area Code": "F",
		"location_type": "Martial Arts",
		"postcode": "1096AN"
	},
	{
		"location_name": "DanceDance",
		"Area Code": "A",
		"location_type": "Dance",
		"postcode": "1093JX"
	},
	{
		"location_name": "CrossFit Box",
		"Area Code": "A",
		"location_type": "CrossFit",
		"postcode": "1019AJ"
	},
	{
		"location_name": "Gym A",
		"Area Code": "C",
		"location_type": "Fitness",
		"postcode": "1098NJ"
	}];
  
var result = [];  
for (var i in data) {
  if(data[i]["Area Code"] == "C" && data[i]["location_type"] == "Fitness") {
    result.push(data[i]);
  }
};

console.log(result);

<强> 2。使用JavaScript Array.filter循环:

{{1}}

相关问题