This is my first post : I've been looking for a solution for quite a long time.
这是我的第一篇文章:我一直在寻找解决方案很长一段时间。
So I have this JSON data :
所以我有这个JSON数据:
var object = [{
"nid": "31",
"0": {
"tid": "20",
"name": "Bench Press",
"objectDate": "2012-02-08",
"goal": "rep",
"result": "55.00",
"comments": "sick!",
"maxload": "250"
},
"1": {
"tid": "22",
"name": "Back Squat",
"objectDate": "2012-02-08",
"goal": "time",
"result": "8.00",
"comments": "i was tired.",
"maxload": "310"
}},
{
"nid": "30",
"0": {
"tid": "19",
"name": "Fran",
"objectDate": "2012-02-07",
"goal": "time",
"result": "5.00",
"comments": null
}}];
And I would like to filter it by name. For instance, if I apply a filter for the name "Fran", I'd like to have something like this:
我想按名称过滤它。例如,如果我为名称“Fran”应用过滤器,我想要这样的东西:
[0] => Array
(
[tid] => 19
[name] => Fran
[objectDate] => 2012-02-07
[goal] => time
[result] => 5.00
[comments] =>
)
[1] => Array
(
[tid] => 19
[name] => Fran
[objectDate] => 2012-02-08
[goal] => rep
[result] => 55.00
[comments] => woohoo!
)
Is it possible to achieve? Any help would be greatly appreciated! :>
有可能实现吗?任何帮助将不胜感激! :>
5 个解决方案
#1
9
There is no function for this in Javascript. You have to write your own function like this.
在Javascript中没有这个功能。你必须像这样编写自己的函数。
var arr = [{"nid":"31","0":{"tid":"20","name":"Bench Press","objectDate":"2012-02-08","goal":"rep","result":"55.00","comments":"sick!","maxload":"250"},"1":{"tid":"22","name":"Back Squat","objectDate":"2012-02-08","goal":"time","result":"8.00","comments":"i was tired.","maxload":"310"}},{"nid":"30","0":{"tid":"19","name":"Fran","objectDate":"2012-02-07","goal":"time","result":"5.00","comments":null}}];
function filterByProperty(array, prop, value){
var filtered = [];
for(var i = 0; i < array.length; i++){
var obj = array[i];
for(var key in obj){
if(typeof(obj[key] == "object")){
var item = obj[key];
if(item[prop] == value){
filtered.push(item);
}
}
}
}
return filtered;
}
var byName = filterByProperty(arr, "name", "Fran");
var byGoal = filterByProperty(arr, "goal", "time");
#2
6
I would create a function for filtering :
我会创建一个过滤功能:
function filter(array, key, value){
var i, j, hash = [], item;
for(i = 0, j = array.length; i<j; i++){
item = array[i];
if(typeof item[key] !== "undefined" && item[key] === value){
hash.push(item);
}
}
return hash;
}
A more robust solution might be adding a filter method to the prototype:
更强大的解决方案可能是为原型添加过滤方法:
`This prototype is provided by the Mozilla foundation and
is distributed under the MIT license.
http://www.ibiblio.org/pub/Linux/LICENSES/mit.license`
if (!Array.prototype.filter)
{
Array.prototype.filter = function(fun /*, thisp*/)
{
var len = this.length;
if (typeof fun != "function")
throw new TypeError();
var res = new Array();
var thisp = arguments[1];
for (var i = 0; i < len; i++)
{
if (i in this)
{
var val = this[i]; // in case fun mutates this
if (fun.call(thisp, val, i, this))
res.push(val);
}
}
return res;
};
}
Then simply call:
然后简单地调用:
function filterName (item, index, array) {
return (item.name === "Fran");
}
var result = object.filter(filterName);
#3
5
var result = [];
for (var i = 0; i < object.length; i++)
{
if (object[i].name == 'Fran')
{
result.push(object[i]);
}
}
#4
0
The big trick is to make a flat array with just the item you want in it.
最大的诀窍是制作一个平面阵列,只需要你想要的项目。
say you have a 2d array like so:
假设你有一个像这样的二维数组:
e = [[1,2],[1,2],[2,3],[4,5],[6,7]]
you make a new array:
你创建一个新的数组:
var f = []
fill it with just 1 item:
填写它只有1项:
for(var x=0;x<e.length;x++) f[x] = e[x][1]
giving us the likes of:
给我们这样的喜欢:
f = [2,2,3,5,7]
and then....
接着....
var g = e.filter( function(elem, pos){ return (f.indexOf(elem[1]) == pos) })
cowabunga!
cowabunga!
#5
0
I try the solution from Diode. I adapt for my case. There are 3 arrays included.
我尝试二极管的解决方案。我适应了我的情况。包括3个阵列。
var arr =
[
{
taskTypeGroupId: "EXP.CONT", taskTypeGroupName: "Contradictoire",
taskType:
{
taskTypeId: "DGE-EXPCONT", taskTypeName: "Dégats des eaux contradictoire", defaultDuration: 60, isInProject: false,
dataItems:
{
id: "EXTRAFILLER5", label: "Divers 5"
}
}
},
{
takTypeGroupId: "EXPQUAL", taskTypeGroupName: "Contrôle qualité",
taskType:
{
taskTypeId: "DGE-EXPQUAL", taskTypeName: "Contrôle qualité dégats des eaux", defaultDuration: 60, isInProject: false,
dataItems:
{
id: "EXTRAFILLER5", label: "Divers 5"
}
}
}
];
function filterByProperty(array, prop, value){
var filtered = [];
for(var i = 0; i < array.length; i++){
var array1 = array[i];
for(var key in array1){
if(typeof(array1[key] == "object")){
var array2 = array1[key];
for (var key2 in array2){
if(typeof(array2[key2] == "object")){
var array3 = array2[key2];
if(array3[prop] == value){
filtered.push(array3);
}
}
}
}
}
}
return filtered;
}
}
JsBin的例子
#1
9
There is no function for this in Javascript. You have to write your own function like this.
在Javascript中没有这个功能。你必须像这样编写自己的函数。
var arr = [{"nid":"31","0":{"tid":"20","name":"Bench Press","objectDate":"2012-02-08","goal":"rep","result":"55.00","comments":"sick!","maxload":"250"},"1":{"tid":"22","name":"Back Squat","objectDate":"2012-02-08","goal":"time","result":"8.00","comments":"i was tired.","maxload":"310"}},{"nid":"30","0":{"tid":"19","name":"Fran","objectDate":"2012-02-07","goal":"time","result":"5.00","comments":null}}];
function filterByProperty(array, prop, value){
var filtered = [];
for(var i = 0; i < array.length; i++){
var obj = array[i];
for(var key in obj){
if(typeof(obj[key] == "object")){
var item = obj[key];
if(item[prop] == value){
filtered.push(item);
}
}
}
}
return filtered;
}
var byName = filterByProperty(arr, "name", "Fran");
var byGoal = filterByProperty(arr, "goal", "time");
#2
6
I would create a function for filtering :
我会创建一个过滤功能:
function filter(array, key, value){
var i, j, hash = [], item;
for(i = 0, j = array.length; i<j; i++){
item = array[i];
if(typeof item[key] !== "undefined" && item[key] === value){
hash.push(item);
}
}
return hash;
}
A more robust solution might be adding a filter method to the prototype:
更强大的解决方案可能是为原型添加过滤方法:
`This prototype is provided by the Mozilla foundation and
is distributed under the MIT license.
http://www.ibiblio.org/pub/Linux/LICENSES/mit.license`
if (!Array.prototype.filter)
{
Array.prototype.filter = function(fun /*, thisp*/)
{
var len = this.length;
if (typeof fun != "function")
throw new TypeError();
var res = new Array();
var thisp = arguments[1];
for (var i = 0; i < len; i++)
{
if (i in this)
{
var val = this[i]; // in case fun mutates this
if (fun.call(thisp, val, i, this))
res.push(val);
}
}
return res;
};
}
Then simply call:
然后简单地调用:
function filterName (item, index, array) {
return (item.name === "Fran");
}
var result = object.filter(filterName);
#3
5
var result = [];
for (var i = 0; i < object.length; i++)
{
if (object[i].name == 'Fran')
{
result.push(object[i]);
}
}
#4
0
The big trick is to make a flat array with just the item you want in it.
最大的诀窍是制作一个平面阵列,只需要你想要的项目。
say you have a 2d array like so:
假设你有一个像这样的二维数组:
e = [[1,2],[1,2],[2,3],[4,5],[6,7]]
you make a new array:
你创建一个新的数组:
var f = []
fill it with just 1 item:
填写它只有1项:
for(var x=0;x<e.length;x++) f[x] = e[x][1]
giving us the likes of:
给我们这样的喜欢:
f = [2,2,3,5,7]
and then....
接着....
var g = e.filter( function(elem, pos){ return (f.indexOf(elem[1]) == pos) })
cowabunga!
cowabunga!
#5
0
I try the solution from Diode. I adapt for my case. There are 3 arrays included.
我尝试二极管的解决方案。我适应了我的情况。包括3个阵列。
var arr =
[
{
taskTypeGroupId: "EXP.CONT", taskTypeGroupName: "Contradictoire",
taskType:
{
taskTypeId: "DGE-EXPCONT", taskTypeName: "Dégats des eaux contradictoire", defaultDuration: 60, isInProject: false,
dataItems:
{
id: "EXTRAFILLER5", label: "Divers 5"
}
}
},
{
takTypeGroupId: "EXPQUAL", taskTypeGroupName: "Contrôle qualité",
taskType:
{
taskTypeId: "DGE-EXPQUAL", taskTypeName: "Contrôle qualité dégats des eaux", defaultDuration: 60, isInProject: false,
dataItems:
{
id: "EXTRAFILLER5", label: "Divers 5"
}
}
}
];
function filterByProperty(array, prop, value){
var filtered = [];
for(var i = 0; i < array.length; i++){
var array1 = array[i];
for(var key in array1){
if(typeof(array1[key] == "object")){
var array2 = array1[key];
for (var key2 in array2){
if(typeof(array2[key2] == "object")){
var array3 = array2[key2];
if(array3[prop] == value){
filtered.push(array3);
}
}
}
}
}
}
return filtered;
}
}
JsBin的例子