How can i update the following JSON object dynamically using javascript or Jquery?
var jsonObj = [{'Id':'1','Username':'Ray','FatherName':'Thompson'},
{'Id':'2','Username':'Steve','FatherName':'Johnson'},
{'Id':'3','Username':'Albert','FatherName':'Einstein'}]
I would like to dynamically update the Username to ‘Thomas’ where the ‘Id’ is ‘3’.
How can I achieve this?
A plain JavaScript solution, assuming
jsonObjalready contains JSON:Loop over it looking for the matching Id, set the corresponding Username, and
breakfrom the loop after the matched item has been modified:Here it is on jsFiddle.
Here’s the same thing wrapped in a function: