You can loop through all enumerable string properties (including inherited once) with for...in. You can combine this with a regex match to check if the key meets your requirements.
const data = {
"pebble" : { "status": "active" },
"stone" : { "status": "active" },
"stone_ny" : { "status": "active" },
"stone_london": { "status": "active" },
"stone_tokyo" : { "status": "active" },
};
const result = {};
for (const key in data) {
if (key.match(/^stone_/)) result[key] = data[key];
}
console.log(result);
However if you are currently already using a library you could check if there is a helper present. A common name would be pickBy()
which accepts the object and predicate (test function).
In Lodash:
const result = _.pickBy(data, (_, key) => key.match(/^stone_/));
In Ramda:
const result = R.pickBy((_, key) => key.match(/^stone_/), data);
// or: const result = R.pickBy(R.flip(R.test(/^stone_/)), data);
Some libraries might also use filter()
and return an object if the input was an object.