filter object by key and its items

前端 未结 2 1387
猫巷女王i
猫巷女王i 2021-01-20 21:17

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

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

let myKeys = Object.keys(data).filter(fun         


        
相关标签:
2条回答
  • 2021-01-20 21:25

    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]), []);
    
    0 讨论(0)
  • 2021-01-20 21:32

    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.

    0 讨论(0)
提交回复
热议问题