javascript 按键及其项目过滤对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45833702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 06:16:46  来源:igfitidea点击:

filter object by key and its items

javascriptecmascript-6vue.js

提问by hauge

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

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

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

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

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:

对象 im 过滤与此类似:

{
"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"
    }
]

}

}

回答by Nina Scholz

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

Array#filter期望布尔值作为返回值,您可以使用它

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]), []);

回答by Thijs

If you have the proper key(s), you can just get the property from the object.

如果您有正确的键,则可以从对象中获取属性。

myKeys.forEach(key => {
   console.log(data[key]);
});

This will print the object whose keys you've filtered earlier.

这将打印您之前过滤其键的对象。