0

In javascript, how can I take an object's values:

var fieldsObject = ['contentA', 'contentB', 'contentC', 'contentD'];

and plug them in as keys of another object?

result[contentA][contentB][contentC][contentD]

My inclination is to loop, but not I'm not seeing how to construct this.

Gor
  • 505
  • 1
  • 6
  • 18
  • can u give more details on what u trying to accomplish – Nakib Oct 28 '15 at 17:22
  • 1
    what do you expect for it's value ? do u want something like result[contentA]=value1, result[contentB]=value2 or something like result[contentA][contentB].... = some_value. for 1st case do like, ` var result={} for(key in fieldObject){ result[key] = value // value 1,2,... } ` – Aishwat Singh Oct 28 '15 at 17:23
  • Someone posted an answer, now it's gone. However, it works for what I'm doing. For the record, here's my 'accepted answer': `function accessObject(mainObject, fieldsObjects) { var access = mainObject; while (fieldsObjects.length > 0) { var key = fieldsObjects.shift(); if (access[key] !== undefined) { access = access[key]; } else { break; } } return access; }` – Gor Oct 28 '15 at 17:40
  • Thanks for your help @Nakib. What worked for me is posted above. – Gor Oct 28 '15 at 17:48

0 Answers0