3

Yeh..I know its not that much related to technical. Its absolutely logical But I'm stuck here.

Lets consider i have an array 1) arr = ['bno', 'am', 'recruiter'];

Expected Output

Obj = {
     key_name: 'bno',
     emplist = [],
     children_obj: {
        key_name: 'am',
        emplist = [],
        children_obj: {
          key_name: 'recruiter',
          emplist = [],
        }
     }

};

2) arr = ['am', 'recruiter'];

Output

Obj = {
         key_name: 'am',
         emplist = [],
         children_obj: {
            key_name: 'recruiter',
            emplist = [],
         }
   };

Anybody can please suggest or provide best way to create such dynamic object based on array element. Would help me out to get it.

Thanks in advance Cheers

5
  • Stack Overflow is not a free code writing service. Next time, please show your code/effort and what the actual problem is. Commented Oct 26, 2017 at 14:05
  • jsbin.com/mazofegejo/2/edit?html,js,console try this and watch in console Commented Oct 26, 2017 at 14:07
  • It's easy with a short, recursive function: jsfiddle.net/xp3zazmo Or a non-mutating version: jsfiddle.net/xp3zazmo/1 Commented Oct 26, 2017 at 14:09
  • @Cerbrus: Noted Commented Oct 26, 2017 at 14:12
  • Here's one that uses a rest parameter to separate the head of the list from the tail. jsfiddle.net/xp3zazmo/2 Commented Oct 26, 2017 at 14:18

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.