我有一个ajax请求,它返回给我一个看起来大致如下的对象:
[
{category: "Test Category", id: "1", name: "Test", language: "English"},
{category: "Test Category", id: "2", name: "Test2", language: "English"},
{category: "A test", id: "3", name: "Test3", language: "Spanish"},
{category: "Test Category", id: "4", name: "Test4", language: "Spanish"}
]
基本上我需要一种方法来执行以下操作:
现在我很确定这样做的最佳途径是创建我自己的临时数组,可以容纳这些数据,但我对Javascript没有足够的信心来编写正确的循环来获取数据。我知道在PHP中我会在循环时执行类似$category['Test Category'][] = 3
的操作。
我的想法是,我需要能够根据用户选择按英语过滤然后按其中的类别过滤事物。
有人能指出我正确的方向吗?
答案 0 :(得分:3)
为您的任务使用下划线的一些方法: http://underscorejs.org
var data = [
{category: "Test Category", id: "1", name: "Test", language: "English"},
{category: "Test Category", id: "2", name: "Test2", language: "English"},
{category: "A test", id: "3", name: "Test3", language: "Spanish"},
{category: "Test Category", id: "4", name: "Test4", language: "Spanish"}
]
// Filter by spanish
var output = _.where(data, {"language":"Spanish"});
// Filter by category
var output = _.where(data, {"category":"A test"});
// List specific category ids
var output = _.pluck(_.where(data, {"category":"A test"}), "id");
// List category names
var output = _.pluck(_.where(data, {"language":"English"}), "category");
// Group by languages
var output = _.groupBy(data, function(value) {
return value.language
});
output["English"];
答案 1 :(得分:2)
由于你在标签中包含了jQuery,我以为我会用它:
//All categories with English or spanish language (categories is the array)
$.grep(categories, function(c) { return c.language=='English' || c.language == 'Spanish'; });
答案 2 :(得分:1)
使用vanilla JS,其中json
是您的原始数组,您可以使用:
function filterJson(attr, obj) {
var attrList = [],
match,
tempAttr,
i;
for (i = 0; i < json.length; i += 1) {
match = true;
for (tempAttr in obj) {
if (!obj[tempAttr].test(json[i][tempAttr])) {
match = false;
break;
}
}
if (match) attrList.push(json[i][attr]);
}
return attrList;
}
并像这样使用它:
“列出仅以英语或西班牙语存在的所有类别”
filterJson('category', {
language: /(English|Spanish)/
});
// => ["Test Category", "Test Category", "A test", "Test Category"]
“列出以指定语言存在的所有ID”
filterJson('id', {
language: /English/
});
// => ["1", "2"]
“列出指定类别中存在的所有ID”
filterJson('id', {
category: /Test Category/
});
// => ["1", "2", "4"]
答案 3 :(得分:0)
如果你可以依赖一些关于数据总是正确的假设 - 例如,你将只有类别:,id:,name:和language:,并且永远不会有冒号或逗号在名称或类别中的任何地方 - 然后我实际上建议将所有信息都粘贴到单个字符串中并使用RegExp来查找它。
例如:
[
"category:Test Category, id:1, name:Test, language:English",
"category:Test Category, id:2, name:Test2, language:English",
"category:A test, id:3, name:Test3, language:Spanish",
"category:Test Category, id:4, name:Test4, language:Spanish"
]
现在,要查找条目的值,请使用RegExp.match()。
var m = /^category:([^,]+)\s+id:(\d+),\s+name:([^,]+),\s+language:(.+)$/i;
// m[1] = Test Category
// m[2] = 1;
// m[3] = Test2
// m[4] = Spanish
您可以使用测试进行搜索。
function my_filter(criteria) {
var results = [];
var re = new RegExp(criteria);
for (var i=0; i<array.length; i++) {
if (array[i].test(re))
results.push(array[i]);
}
return results;
}
var english_array = my_filter('language:english');
var atest_array = my_filter('category:a test');
答案 4 :(得分:-1)
神奇的Google术语是“地图”和“缩小”。
var data = [...the JSON data parsed with JSON.parse() if necessary...]
var categoriesInEnglish = data.reduce(function(accumulator, category) {
if(category.language = 'English') {
accumulator.push(category.id);
}
});
请参阅http://www.mrspeaker.net/2011/05/02/conventional-interfaces/和http://www.mrspeaker.net/2011/04/27/reducing-map/