简体   繁体   English

如何从对象数组中仅获取真值键对

[英]How to get only true value key pair from array of objects

I have an array of object that is coming from some api.The data i am getting is like this.我有一个来自一些 api 的 object 数组。我得到的数据是这样的。 It has multiple values but i only want to show the user which access he has.它有多个值,但我只想向用户显示他拥有的访问权限。 Suppose a user have only read access so i want to show the user read key.假设用户只有读取权限,所以我想显示用户读取密钥。

                    [
                      {
                        admin: false,
                        createdAt: "2022-08-21T05:32:20.936Z",
                        id: 8,
                        read: false,
                        write: true,
                      },
                    ];

So, i want to get only the key value pair from this array which has true values so i can show the keys using Object.keys() .因此,我只想从此数组中获取具有真值的键值对,以便我可以使用Object.keys()显示键。

expected result预期结果

[{write:true}]

I have tried different array methods but didn't succeed, here how i was thinking to solve this problem but it's only returning last element value.我尝试了不同的数组方法但没有成功,这里我想如何解决这个问题,但它只返回最后一个元素值。

item.map(tab => {
        return Object.keys(tab).reduce((acc: string, key) => {
          if (tab[key]) {
            acc[key] = tab[key];
          }
          return acc;
        }, {});
      }),

if (tab[key]) will be applied on any truthy value not just true , for example, not empty string is a truthy value, any number is a truthy value except zero. if (tab[key])将应用于任何真值,而不仅仅是true ,例如,非空string是真值,任何数字都是除零以外的真值。

So you need explicitly check if the value equal to true by if (tab[key] === true)因此,您需要通过if (tab[key] === true)显式检查值是否等于true

 const data = [ { admin: false, createdAt: "2022-08-21T05:32:20.936Z", id: 8, read: false, write: true, }, ]; const result = data.map(tab => { return Object.keys(tab).reduce((acc, key) => { if (tab[key] === true) { acc[key] = tab[key]; } return acc; }, {}); }) console.log(result)

For shorthand use can use对于速记使用可以使用

const result = data.map(tab => Object.entries(tab).reduce((acc, [key, value]) => ({ ...acc, ...(value === true && { [key]: value }) }), {}));

You can get rid of reduce by creating an object from filtered entries.您可以通过从过滤的条目创建 object 来摆脱 reduce。 Then just filter by true values.然后只需按真实值过滤即可。

 data = [ { admin: false, createdAt: "2022-08-21T05:32:20.936Z", id: 8, read: false, write: true, }, { admin: false, createdAt: "1234", id: 8, read: true, write: true, } ]; out = data.map(item => Object.fromEntries(Object.entries(item).filter(([key, value]) => value === true))); console.log(out)

You can get the keys you want by changing the 2nd parameter of the keyFilters function.您可以通过更改 keyFilters function 的第二个参数来获得所需的密钥。

let tabs = [
  {admin: false,createdAt: "2022-08-21T05:32:20.936Z",id: 8,read: false,write: true},
  {admin: false,createdAt: "2022-08-21T05:32:20.936Z",id: 8,read: true,write: true}
  ];
let keyFilters = function(values, keys){
  let filteredKeys = {}
  Object.keys(values).map((key, index)=>{
    if (keys.includes(key)){
       filteredKeys[key] = values[key]
    }
  }) 

  return filteredKeys;
}


let filters = tabs.map(tab=>keyFilters(tab, ["read", "write"]))
console.log(filters)

output output

0:(2) {read: false, write: true} 1:(2) {read: true, write: true 0:(2){读:假,写:真} 1:(2){读:真,写:真

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM