Let me add step by step comments in your code, to explain what is going on at the step you're having trouble understanding!
var data = ["avinash", "supte"]
var dataLength = data.length;
var index = {};
for (var i = 0; i < dataLength; i++){
var entry = data[i]; // entry will be "avinash"
if (entry === undefined) {
continue;
}
var str = getWords(entry); // str will be ["avinash"]
for (var j = 0; j < str.length; j++) {
var item = str[j]; // item will be "avinash"
var itemLength = item.length - 1;
var node = index;
for (var n = 0; n < itemLength; n++) {
// char will be "a" in the 1st iteration
var char = item[n];
var newNode = node[char];
newNode = newNode === undefined ? {} : newNode;
node[char] = newNode;
// At this point node will be { a: {} }
node = newNode;
// What's happening in the above step is...
// node is being set to the obj. represented by...
// the key "a" in the index object, which is { a: {} }
// Think of it this way, when you do:
// node[char] = newNode ... you're essentially saying
// node.a = {} which will make node equal to { a: {} }
// ...and finally when you do:
// node = newNode, you're saying
// node = node.a
// This process of assigning inner most nested obj.
// trickles down the loop!
// So, at the end of the second iteration index will be...
// { a: { v: {} } } ... and when you do
// node = newNode at the end of the second iteration
// node will be set to the obj. represented by...
// the key "v" in the index object, which is { a: { v: {} } }
}
console.log(index);
}
}
Hope that makes sense, if not don't hesitate to let me know! :)
Having said that, I would say the above code can be refactored to make its intent, and readability much more clearer! I was able to quickly understand it, only because I understood what you were trying to do thanks to one of your earlier questions!