I have a hash like the one below
aa: { categories: { cat1: 'alpha' } }
Starting from the string 'aa.categories.cat1', how can I get alpha suing plain JS?
'aa.categories.cat1'
alpha
Using split() and reduce():
split()
reduce()
const result = path.split('.').reduce((a, v) => a[v], object);
Complete snippet:
const object = { aa: { categories: { cat1: 'alpha' } } } const path = 'aa.categories.cat1'; const result = path.split('.').reduce((a, v) => a[v], object); console.log(result);
1.4m articles
1.4m replys
5 comments
57.0k users