I understand that only array elements can be .push()'ed onto an array.
I want to accomplish the equivalent for an object.
I know the dot and bracket notation, so it's not that old question. I need to do it with variables in a loop.
I've looked into Object.assign() but can't seem to make that work.
Below function is called repeatedly. I need to normalize the data fed to the function into a mini database using a js object.
The last line is the crux. This will (of course) keep overwriting divContent[section].items[idValue].
How do I get it to "push" the record object into the key represented by items[idValue]?
I want to get { itemsKey: { k1:v1, k2:v2, k3:v3 } }. Thanks!
There is no need to maintain index order. The items need to be called later as itemsKey[k1] for example.
function createItemDb (section, fileName, fileContent) {
var fileExt = fileName.split('.')[1].toLowerCase();
fileType = ((fileExt == 'caption') || (fileExt == 'url')) ? fileExt : 'img';
if (fileType == 'img') {
var regExp = /\((.*?)\)/g;
var matches = regExp.exec(fileName);
fileType = matches[1].toLowerCase();
}
var regRemoveParenIncl = /\([^)]*\)/;
var regRemoveExt = /\.[^\/.]+$/;
var idValue = fileName.replace(regRemoveParenIncl,'').replace(regRemoveExt,'');
record = {};
record[fileType] = fileName;
divContent[section].items[idValue] = record;
}