【问题标题】:as3 json object filteringas3 json对象过滤
【发布时间】:2018-02-28 12:29:29
【问题描述】:

我有一个像这样的对象数组。在javascript中,我可以过滤和映射这些东西以获得我的结果。我完全迷失在获取年龄 >= 20 && 年龄

问候

 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】:

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

    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);
    

    您可以在 Adob​​e 文档中找到更多信息:Array::filter

    【讨论】:

    • 不确定我是否理解将年龄传递给函数是什么意思。过滤功能等于手动遍历数组。它将为数组中的每个项目调用byAge 函数。 (与 js 中的方式相同。)
    • 类似这样的东西:blog.code4hire.com/2010/09/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    相关资源
    最近更新 更多