Filter object array by key

Asked

Viewed 308 times

0

At the moment it is only returning if the string to be queried is exactly equal to the key of the object

let estilos = { "alignContent": [ { "ativo": false } ], "alignItems": [ { "ativo": false } ], "alignSelf": [ { "ativo": false } ], "alignmentBaseline": [ { "ativo": false } ], "all": [ { "ativo": false } ], "animation": [ { "ativo": false } ], "animationDelay": [ { "ativo": false } ], "animationDirection": [ { "ativo": false } ], "animationDuration": [ { "ativo": false } ], "animationFillMode": [ { "ativo": false } ], "animationIterationCount": [ { "ativo": false } ], "animationName": [ { "ativo": false } ], "animationPlayState": [ { "ativo": false } ], "animationTimingFunction": [ { "ativo": false } ], "backfaceVisibility": [ { "ativo": false } ], "background": [ { "ativo": false } ], "backgroundAttachment": [ { "ativo": false } ], "backgroundBlendMode": [ { "ativo": false } ] };

const allowed = 'alignContent';

const filtered = Object.keys(estilos)
  .filter(key => allowed.match(new RegExp(key, 'g')))
  .reduce((obj, key) => {
    obj[key] = estilos[key];
    return obj;
  }, {});
  
console.log(filtered);

  • What would be the question?

  • Already solved, I will edit to stay as reference

  • 1

    Post an answer and edit the question by asking which problem was not posed

  • 1

    the solution to the question is not correct... the question is the problem, the solution should be an answer

  • Reverses this last change and adds the answer in the reply box. Then just mark as solved. The person who has a similar doubt, when entering here, will be "lost" without knowing how to do too.

  • really, edited

Show 1 more comment

1 answer

1


allowed and key should have their seats reversed, so the query if by each letter, it is also good to add the method toLowerCase to make the search independent of upper or lower case...

let estilos = { "alignContent": [ { "ativo": false } ], "alignItems": [ { "ativo": false } ], "alignSelf": [ { "ativo": false } ], "alignmentBaseline": [ { "ativo": false } ], "all": [ { "ativo": false } ], "animation": [ { "ativo": false } ], "animationDelay": [ { "ativo": false } ], "animationDirection": [ { "ativo": false } ], "animationDuration": [ { "ativo": false } ], "animationFillMode": [ { "ativo": false } ], "animationIterationCount": [ { "ativo": false } ], "animationName": [ { "ativo": false } ], "animationPlayState": [ { "ativo": false } ], "animationTimingFunction": [ { "ativo": false } ], "backfaceVisibility": [ { "ativo": false } ], "background": [ { "ativo": false } ], "backgroundAttachment": [ { "ativo": false } ], "backgroundBlendMode": [ { "ativo": false } ] };

const allowed = 'align';

const filtered = Object.keys(estilos)
  .filter(key => key.match( new RegExp(allowed, 'gi')))
  .reduce((obj, key) => {
    obj[key] = estilos[key];
    return obj;
  }, {});
  
console.log(filtered);

  • 1

    I didn’t even need to use toLowerCase()... since you are using regex it makes more sense to use the "i" flag. But if you are happy like this, then

  • guy I have a serious problem with regex kkk, thanks

Browser other questions tagged

You are not signed in. Login or sign up in order to post.