MTK MTK - 4 months ago 9
Javascript Question

javascript check object properties begining with

How to select only the key's in object begining with certain character?
In mi case the key's begining with a dot ('.prop_2' and '.prop_4')

mi_obj = {
'prop_1': 'value 1',
'.prop_2': 'value 2',
prop_3: 'value 3',
'.prop_4': 'value 4'
}


I know it's possible with a "for" loop but no other better option?

Answer

Could do a filter on the keys, which will return an array of the keys that start with dot

    Object.keys(mi_obj).filter(function(key) {
      return key.starsWith('.');
    }