1

I have a json like as follow

[{
    "name": "abc",
    "Path": "i.abc",
    "count": 5347,
    "subFolders": []
},
{
    "name": "cde",
    "Path": "i.cde",
    "count": 0,
    "subFolders": [{
        "name": "efg",
        "Path": "",
        "count": 0,
        "subFolders": []
    },
    {
        "name": "hij",
        "Path": "i.hij",
        "count": 1,
        "subFolders": []
    }]
}]

I want to change "count" value based on "path"(its unique) value. For example I want to change count as 2 for path "i.hij" like that. Following is the code i have tried.

var json = "above json";
  for (i=0; i < json.length; i++) {
      this.updateJson(json[i], path, count);
  }

  updateJson: function(json, path, count) {

    if (json.path == path) {
        json.count = count;
        return json;
    } 

    if (json.subFolders != null && json.subFolders.length > 0) {

      for(j=0; j < json.subFolders.length; j++) {

          this.updateJson(json.subFolders[j], path, count);
      }
    }
  }

How do i get the whole json with modified value?

The Hungry Dictator
  • 3,444
  • 5
  • 37
  • 53
user1606656
  • 63
  • 1
  • 2
  • 8
  • 1
    Your code appears to be incomplete (the `updateJson: function(json, path, count) {` at this position is strange). Also, the "problem" doesn't seem to have anything to do with JSON at all. – Felix Kling Oct 10 '13 at 06:05
  • Your `json` variable appears to be a JS object, not a JSON string. Please name it in a less confusing way. – Denys Séguret Oct 10 '13 at 06:07
  • duplicate of [Javascript: How to set object property given its string name](http://stackoverflow.com/q/13719593/218196). – Felix Kling Oct 10 '13 at 06:09
  • @FelixKling As OP's code was not so far from the goal, I made an answer. It's probably better for him, in fact, than copying an already made code, especially as he has a `Path` field in his nodes. – Denys Séguret Oct 10 '13 at 06:19

1 Answers1

1

You had a few problems in your code, mainly you were confusing Path and path (JavaScript is case sensitive) and you were missing the var keyword in the for (this one is subtle and very dangerous), but you weren't that far from the goal.

Here's a fixed function :

var obj = [{
    "name": "abc",
    "Path": "i.abc",
    "count": 5347,
    "subFolders": []
},
{
    "name": "cde",
    "Path": "i.cde",
    "count": 0,
    "subFolders": [{
        "name": "efg",
        "Path": "",
        "count": 0,
        "subFolders": []
    },
    {
        "name": "hij",
        "Path": "i.hij",
        "count": 1,
        "subFolders": []
    }]
}];

function upd(o, path, count) {
  if (o.Path == path) {
        o.count = count;
  } else {
    var arr;
    if (Array.isArray(o)) arr = o;
    else if (o.subFolders) arr = o.subFolders;
    else return;
    for(var j=0; j < arr.length; j++) {
        upd(arr[j], path, count);
    }
  }
}
upd(obj, "i.hij", 3);
console.log(obj);

I also changed the variable names to remove all references to JSON, as there's no JSON here.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758