2

I have an object like this...

"data": {

   "1": {
      "id": 32,
      "name": "E",
      "link": "", 
    },
    "2": {
      "id": 33,
      "name": "EC",
      "link": "", 
    },

}

how can I set each "link" value using the vars above ? for example I would like to do some thing like

"data": {

  "1": {
     "id": 32,
     "name": "E",
     "link": "id="+this.id+" and name="+this.name, 
  },
  "2": {
     "id": 33,
     "name": "EC",
     "link": "id="+this.id+" and name="+this.name, 
  },

To make the object like this...

"data": {

  "1": {
     "id": 32,
     "name": "E",
     "link": "id=32 and name=E", 
  },
  "2": {
     "id": 33,
     "name": "EC",
     "link": "id=33 and name=EC", 
  },
user3102529
  • 63
  • 2
  • 9
  • What's wrong with your attempt? Did you try the code you were thinking of? – Sterling Archer Mar 21 '16 at 13:19
  • What have you tried? Do you have a loop that you've set up to perform this action? – Joseph Marikle Mar 21 '16 at 13:19
  • you could make an accessor property or compute it after the fact. – Daniel A. White Mar 21 '16 at 13:19
  • I get: id="" and name=undefined – user3102529 Mar 21 '16 at 13:23
  • @JosephMarikle I do not have a loop I just have a long object from id 1 - id 200, i was going to manually put in "link": "id=33 and name=EC" and so on but thought a string replace with this.name for all "link" would be quicker – user3102529 Mar 21 '16 at 13:28
  • @SterlingArcher yes I have tried the above but "link" value outputs id="" and name=undefined – user3102529 Mar 21 '16 at 13:29
  • @user3102529 That's kind of my point. You need a loop. Call it hint if you want, but if you're wanting to dynamically set the link based on id and name, you should be doing it in a loop. – Joseph Marikle Mar 21 '16 at 13:31
  • @JosephMarikle This is third party software that I have brought a licence for and this is the config file, im just trying to update it the quickest way possible. my links are actually going to be sonelink.php?id=33&name=EC I dont want to edit the files too much incase I break anything. – user3102529 Mar 21 '16 at 13:35
  • I have added two strategies below which are both dynamic solutions. – Mr. Polywhirl Mar 21 '16 at 13:57
  • @user3102529 Your solution is in one or more of the answers below. It's already marked as duplicate and it's not really a good question anyway, but here is Nina's answer in a jsfiddle: https://jsfiddle.net/h40aoaLr/1. Just cut and paste your object in there, hit run in the top left corner, and copy the resulting object from the output panel. – Joseph Marikle Mar 21 '16 at 14:00

3 Answers3

1

I suggest to iterate over the properties and change the wanted items.

var object = { data: { "1": { "id": 32, "name": "E", "link": "", }, "2": { "id": 33, "name": "EC", "link": "", } } };

Object.keys(object.data).forEach(function (k) {
    object.data[k].link = 'id=' + object.data[k].id + ' and name=' + object.data[k].name;
});

document.write('<pre>' + JSON.stringify(object, 0, 4) + '</pre>');
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

I typically make it a function:

"data": {

  "1": {
     "id": 32,
     "name": "E",
     "get_link": function() { return "id="+this.id+" and name="+this.name } 
  }

}

Then you access it like whatever.get_link()

mikeb
  • 10,578
  • 7
  • 62
  • 120
0

There are two ways to go about solving this.

You can create a post-processor to process placeholder values. I made a generic pre-processor which should dynamically search for placeholders for all string properties.

var linkStore = {
  "data": {
    "1": {
      "id": 32,
      "name": "E",
      "link": "id={{id}} and name={{name}}"
    },
    "2": {
      "id": 33,
      "name": "EC",
      "link": "id={{id}} and name={{name}}"
    }
  }
};

function postProcess(data, root) {
  var records = root != null ? data[root] : data;
  Object.keys(records).forEach(function(recordId) {
    var record = records[recordId];
    Object.keys(record).forEach(function(property) {
      if (typeof record[property] === 'string') {
        record[property] = record[property].replace(/{{(\w+)}}/g, function(match, value) {
          return record[value];
        });
      }
    });
  });
  return data;
}

var result = postProcess(linkStore, 'data');

document.body.innerHTML = '<pre>' + JSON.stringify(result, null, 2) + '</pre>';

Or if you don't want to modify the original data. You can add a property map to be applied to each of the records.

var linkStore = {
  "data": {
    "1": {
      "id": 32,
      "name": "E",
      "link": ""
    },
    "2": {
      "id": 33,
      "name": "EC",
      "link": ""
    }
  }
};

function postProcess(data, root, propertyMap) {
  var records = root != null ? data[root] : data;
  Object.keys(records).forEach(function(recordId) {
    var record = records[recordId];
    Object.keys(propertyMap).forEach(function(property) {
      record[property] = propertyMap[property].call(record, record[property]);
    });
  });
  return data;
}

var result = postProcess(linkStore, 'data', {
  link : function(oldValue) {
    return 'id=' + this.id + ' and name=' + this.name
  }
});

document.body.innerHTML = '<pre>' + JSON.stringify(result, null, 2) + '</pre>';
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132