0

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;
        }, {});
      }),
callmeizaz
  • 249
  • 3
  • 9

3 Answers3

0

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 }) }), {}));
Mina
  • 14,386
  • 3
  • 13
  • 26
0

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)
Axekan
  • 641
  • 5
  • 11
  • Thanks its working but one more question how do add **,** when rendering these keys coz i want to show it like **read,write** – callmeizaz Aug 21 '22 at 12:59
  • @callmeizaz Well you may need to post another question for that. But for one item, Object.keys(item).toString() will turn the keys into a string with commas. You can read more here: https://stackoverflow.com/questions/39771131/how-to-convert-array-into-comma-separated-string-in-javascript – Axekan Aug 21 '22 at 13:10
0

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

Deniz Aktürk
  • 362
  • 1
  • 9