1

I am referring to this answered question Convert Array into Object. Specifically the answer of @JVE999. Since my reputaion is too low to comment on the answer I am asking a new question.

I don't understand this piece of code. It works like a charm in my code but I simply don't understand why. Could I get a brief walkthrough what each line does and how it actually converts an array into an object?

var convArrToObj = function(array){
    var thisEleObj = new Object();
    if(typeof array == "object"){
        for(var i in array){
            var thisEle = convArrToObj(array[i]);
            thisEleObj[i] = thisEle;
        }
    }else {
        thisEleObj = array;
    }
    return thisEleObj;
}
McMo
  • 151
  • 1
  • 9

2 Answers2

1

Because I'm not sure what you know or don't know, so I'll try to explain every line:

var convArrToObj = function(array){ defines a function with a single parameter, array.

var thisEleObj = new Object(); initializes a new object using a constructor function. var thisEleObj = {}; also works.

if(typeof array == "object"){ ensures that the input is an array or an object. It's not strictly necessary if you know that the input will be an array or an object and that you will not need recursion (see line 5).

for(var i in array){ loops through each "key" in the object. In an array, the keys are all numerical and in numerical order, so for(var i=0;i<array.length;i++){ would be a similar version that only supports arrays.

var thisEle = convArrToObj(array[i]); This is the clever part, and likely the most unclear. It checks if the target property of the object (or index of the array) is an array itself, and copies it as an object if so.

thisEleObj[i]=thisEle is the part that "gets everything done" by copying thisEle(the converted array) to the array.

else { thisEleObj=array} doesn't bother to process datatypes like numbers (who usually don't have properties) or functions (who have properties that should not be processed)

return thisEleObj outputs the processed object to an assignment/another function/another call of itself due to recursion.

Hope this helped, tell me if there's anything I need to clarify.

Ben Rivers
  • 129
  • 10
1

May it can help you :)

    var convArrToObj = function(array){
         var thisEleObj = new Object();
         // when first time it checks for type [1, 2, 3, 4] it return object because type of a object return object
         if(typeof array == "object"){
             for(var i in array){
              // here we are getting each item from array and calling same function convArrToObj(1) and this time will not be object so if condition get false and return same value from  else { thisEleObj = array; } and this value will assigned to a index of object
                 var thisEle = convArrToObj(array[i]);
                 thisEleObj[i] = thisEle;
             }
         }else {
             thisEleObj = array;
         }
         //and finaly thisEleObj will be object when recursive function get called variables inside function creates new scope so thisEleObj will not be override each time. In last when array finished loop final object will be returned yoc can check console()
console.log(thisEleObj);
         return thisEleObj;
     }
    
     convArrToObj([1, 2, 3, 4]);
Pavan Sikarwar
  • 833
  • 5
  • 14