For example if we have an existing object
const mainObject = {
title: 'some title',
topics: {
topic1: {
path: '',
id: 1
},
topic2: {
path: '',
id: 2
}
}
}
and I have a function that gets array containing keys for example
const arrayOfKeys = ['topics', 'topic1'];
function getObjectByKeys(arrayOfKeys) {
// problem is length of the array may change
const myObject = mainObject[arrayOfKeys[0]][arrayOfKeys[1]];
return myObject;
}
function should return
{
path: '',
id: 1
}