Home > Net >  How to get only true value key pair from array of objects
How to get only true value key pair from array of objects

Time:08-22

I have an array of object that is coming from some api.The data i am getting is like this. 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().

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;
        }, {});
      }),

CodePudding user response:

You can get rid of reduce by creating an object from filtered entries. 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)

CodePudding user response:

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.

So you need explicitly check if the value equal to true by if (tab[key] === 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 }) }), {}));

CodePudding user response:

You can get the keys you want by changing the 2nd parameter of the 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

0:(2) {read: false, write: true} 1:(2) {read: true, write: true

  • Related