as3 json 对象过滤
as3 json object filtering
我有一个像这样的对象数组。在 javascript 中,我可以过滤和映射这些东西以获得我的结果。我完全迷失了获取 age >= 20 && age <= 27 的条目。好的,我可以遍历每个对象,但是是否可以使用函数来过滤掉它?
问候
var arr:Array = [
{
categories:["a", "b", "c"],
users:["John", "Steve"],
id:1,
information:{
age:"30",
color:"red"
}
},
{
categories:["d", "e","c"],
users:["Martin", "Jason"],
id:2,
information:{
age:"25",
color:"blue"
}
},
{
categories:["d", "c"],
users:["Robert"],
id:3,
information:{
age:"26",
color:"green"
}
}
]
基本上,您使用的是一组对象,每个对象都有其属性。你可以写一个函数来过滤掉你感兴趣的对象。
function byAge(item: Object, ...rest): Boolean {
const info: Object = item && item.hasOwnProperty('information')) ? item['information'] : null;
if (info && info.hasOwnProperty('age')) {
const age: int = info['age'];
return age >= 20 && age <= 27;
}
return false;
}
...
const filtered: Array = arr.filter(byAge);
您可以在 Adobe 文档中找到更多信息:Array::filter
我有一个像这样的对象数组。在 javascript 中,我可以过滤和映射这些东西以获得我的结果。我完全迷失了获取 age >= 20 && age <= 27 的条目。好的,我可以遍历每个对象,但是是否可以使用函数来过滤掉它?
问候
var arr:Array = [
{
categories:["a", "b", "c"],
users:["John", "Steve"],
id:1,
information:{
age:"30",
color:"red"
}
},
{
categories:["d", "e","c"],
users:["Martin", "Jason"],
id:2,
information:{
age:"25",
color:"blue"
}
},
{
categories:["d", "c"],
users:["Robert"],
id:3,
information:{
age:"26",
color:"green"
}
}
]
基本上,您使用的是一组对象,每个对象都有其属性。你可以写一个函数来过滤掉你感兴趣的对象。
function byAge(item: Object, ...rest): Boolean {
const info: Object = item && item.hasOwnProperty('information')) ? item['information'] : null;
if (info && info.hasOwnProperty('age')) {
const age: int = info['age'];
return age >= 20 && age <= 27;
}
return false;
}
...
const filtered: Array = arr.filter(byAge);
您可以在 Adobe 文档中找到更多信息:Array::filter