I have an Object which contains some booleans like this:
{ date: "2017-10-05", name_change: false, age_change: true, ... }
I want to filter() the keys which are true.
I also need the date value. how can I make this filter?
Regards.
I have an Object which contains some booleans like this:
{ date: "2017-10-05", name_change: false, age_change: true, ... }
I want to filter() the keys which are true.
I also need the date value. how can I make this filter?
Regards.
Use Object.entries() to convert the object to an array of [key, value] tuples. Filter the tuples by check if the value is true. Convert back to an object using Object.fromEntries():
const obj = { 
  date: "2017-10-05", 
  name_change: false, 
  age_change: true 
};
const result = Object.fromEntries(
  Object
    .entries(obj)
    .filter(([, val]) => val !== true)
);
console.log(result);Old answer:
Get the keys with Object#keys, and then iterate the array of keys with Array#reduce, and build a new object that doesn't contain keys which value equals to true:
const obj = { 
  date: "2017-10-05", 
  name_change: false, 
  age_change: true 
};
const result = Object.keys(obj)
  .reduce((o, key) => {
    obj[key] !== true && (o[key] = obj[key]);
    return o;
  }, {});
console.log(result);