简体   繁体   中英

Update the value of a field in a JSON object using Jquery

I have a JSON object:

 [{"file":"post_files/feb641027629b6a7ba78fb47063c6763vid.flv","title":"Sample"},
  {"file":"post_files/74c73cf0e28d6ca431ab4c2e2299f39fbarsandtone.flv","title":"Bars"}]

As you can see, file fields have values like post_file/feb.................flv .

All I want is to append http://divinotech.in/ to all the file fields' values, ie the value should get edited like this:

http://divinotech.in/post_file/feb.................flv

How can I do this?

You could loop through the elements of the array and modify the file property:

var array = [
    { "file": "post_files/feb641027629b6a7ba78fb47063c6763vid.flv", "title":"Sample"}, 
    { "file": "post_files/74c73cf0e28d6ca431ab4c2e2299f39fbarsandtone.flv", "title":"Bars"}
];

for (var i = 0; i < array.length; i++) {
    array[i].file = 'http://divinotech.in/' + array[i].file;
}

// at this stage the array will contain the desired values

And here's a live demo .

Try this

var result=[{"file":"post_files/feb641027629b6a7ba78fb47063c6763vid.flv","title":"Sample"},
  {"file":"post_files/74c73cf0e28d6ca431ab4c2e2299f39fbarsandtone.flv","title":"Bars"}]
for (var i in result) {
$("body").append('<a href="http://divinotech.in/'+result[i].file+'">'+result[i].title+'</a></br>');
} 

DEMO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM