按键及其项过滤对象 [英] filter object by key and its items

查看:70
本文介绍了按键及其项过滤对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象,我想过滤它的键..

I have an object that I would like to filter it's keys..

我试图用ID过滤对象,如下所示:

Im trying to filter the object by an ID, like so:

let myKeys = Object.keys(data).filter(function(key) {
        //console.log(data[key]);
        if(parseInt(key) === parseInt(vm.system_id)) {
            return data[key];
        }
    });

    console.log(myKeys);

这部分工作 - 我获取密钥,但是,我没有得到所有的数据/项目此项目即时过滤

This works, partialy - im getting the key, however, im not getting all the data/items under this item im filtering out

对象即时过滤与此类似:

The object im filtering is one similar to this one:

{
"646": [{
        "id": 52144,
        "timestamp": "2017-08-17T14:10:23Z",
        "type": "alarm",
        "code": 210,
        "title": "",
        "description": "",
        "remedy": "",
        "appeared": "2017-08-17T14:10:09Z",
        "disappeared": null,
        "acknowlegded": null,
        "solved": null,
        "system_name": "CG-MX19D7K5C1",
        "system_id": 646,
        "system_device_id": 458,
        "stream": "cu351.alarm_code"
    }
],
"693": [{
        "id": 51675,
        "timestamp": "2017-08-16T13:59:55Z",
        "type": "alarm",
        "code": 215,
        "title": "",
        "description": "",
        "remedy": "",
        "appeared": "2017-08-16T13:59:57Z",
        "disappeared": null,
        "acknowlegded": null,
        "solved": null,
        "system_name": "Demo 07122016",
        "system_id": 693,
        "system_device_id": 371,
        "stream": "cu351.alarm_code"
    }, {
        "id": 51677,
        "timestamp": "2017-08-16T13:59:57Z",
        "type": "alarm",
        "code": 214,
        "title": "",
        "description": "",
        "remedy": "",
        "appeared": "2017-08-16T13:59:59Z",
        "disappeared": null,
        "acknowlegded": null,
        "solved": null,
        "system_name": "Demo 07122016",
        "system_id": 693,
        "system_device_id": 371,
        "stream": "cu351.alarm_code"
    }
]

}

推荐答案

数组#过滤器 期望一个布尔值作为返回值,你可以使用这个

Array#filter is expecting a boolean value as return value, you might use this

let myKeys = Object.keys(data).filter(key => key == vm.system_id);

获取密钥然后使用给定密钥渲染新对象。

for getting the keys and then render a new object with the given keys.

要获取单个数组中的所有项目,您可以使用

To get all items in a single array, you could collect them with

let result = myKeys.reduce((r, k) => r.concat(data[k]), []);

这篇关于按键及其项过滤对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆