-2

I'm trying to build a new object from an old object recursively like:

var arr = {};   
var markCheckBoxes = function(obj){
    var trackArr = new Array();
    for(var prop in obj){
        if(!!obj[i] && typeof(obj[i])=="object"){
            trackArr.push(i);
            markCheckBoxes(obj[i]);
            trackArr.pop();
        }
        else{
            var str = trackArr.join(".") + "." + i;
            arr[str] = true;
        }
    }
};

But this creates a flat object. For example if obj is:

obj = {
        prop1: {
                subprop1: "text", 
                subprop2: "extra"
        }, prop2:{
                subprop: {another: 3}
        }
}

Then the above function creates an object with keys:

{
    "prop1.subprop1": true,
    "prop1.subprop2": true,
    "prop2.subprop.another": true
}
ayushgp
  • 4,045
  • 3
  • 29
  • 67
  • I'm guessing from `new Array()` your a JS novice. Setting an objects property through `[]` only works one level deep. You cannot set `.` separated properties like your trying to do. EDIT: Down-votes may be because youve stated an issue but not asked any questions about it. – ste2425 Jun 21 '16 at 10:40
  • @ste2425 how can I do it using the list of parents I have gathered in `trackArr`? – ayushgp Jun 21 '16 at 10:42
  • @ste2425 And why would a JS pro not use a `new Array()`? – ayushgp Jun 21 '16 at 10:43
  • What exactly is it your trying to do? I tried to create a fiddle but i have no idea what your method is even meant to do, it looks like your trying to recursively clone, but then your setting things to `true`. You would use object literals `{} [] 'test'` over `new Object() new Array() new String()` for many reasons. That is something google can tell you. – ste2425 Jun 21 '16 at 10:46
  • http://stackoverflow.com/questions/931872/what-s-the-difference-between-array-and-while-declaring-a-javascript-ar – but mostly it's convention; why bother typing `new Array()` when you could just do `[]`? (also, consistency: you don't do `new Object()` either.) – JJJ Jun 21 '16 at 10:46
  • 1
    @ste2425 ok, I'll keep that in mind. What I'm trying to do is create a new object with all the fields in first object set as true in the new object – ayushgp Jun 21 '16 at 10:52

1 Answers1

2

Assuming OP want to make other copy of object with true value for the keys, you can modify the code as below

var makeChecks = function(obj){
  var cloneTo = {};
  for(var prop in obj){
   if(obj.hasOwnProperty(prop) && typeof(obj[prop]) === 'object'){
     cloneTo[prop] = makeChecks(obj[prop]);
   }
   else{
    cloneTo[prop] = true;
   }
  }
  return cloneTo;
}

var obj = {
        prop1: {
                subprop1: "text", 
                subprop2: "extra"
        }, prop2:{
                subprop: {another: 3}
        }
}

var output = makeChecks(obj);

console.log(output) 

/*
 {
 "prop1": {
  "subprop1": true,
  "subprop2": true
 },
 "prop2": {
  "subprop": {
   "another": true
  }
 }
}
*/
Jagdish Idhate
  • 6,302
  • 6
  • 27
  • 46