as3 json对象过滤

问题描述 投票:-1回答:1

我有一个像这样的对象数组。在javascript中我可以过滤和映射这些东西以获得我的结果。我完全迷失了年龄> = 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"
        }
    }
]
json actionscript-3 filter
1个回答
0
投票

基本上你使用一组对象,每个对象都有它的属性。您可以编写一个函数来仅过滤掉您感兴趣的对象。

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

© www.soinside.com 2019 - 2024. All rights reserved.